Search code examples
iosobjective-cuidatepickernstimeinterval

Need assistance regarding converting UIDatePicker time to timestamp


I am trying to convert UIDatePicker hour and minute into timestamp like this

- (IBAction)datePickerValueChanged:(id)sender {
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];

    [outputFormatter setDateFormat:@"hh:mm a"];

    [outputFormatter setTimeZone:[NSTimeZone defaultTimeZone]];

    self.timeLbl.text = [outputFormatter stringFromDate:self.theDatePicker.date];

    NSTimeInterval ti = [self.theDatePicker.date timeIntervalSince1970];

    NSString *sit = [NSString stringWithFormat:@"%f", ti];

    NSLog(@"%@", sit);
}

I want convert UIDatePicker hour and minute to timestamp not the seconds. How to achieve this? because I am testing through this website http://www.epochconverter.com/ but I am setting seconds. I want like 11:05 am/pm not the seconds.


Solution

  • [outputFormatter stringFromDate:self.theDatePicker.date] produces a string you need. Just NSLog this instead of sit.