Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS : Switching between 12/24 hour times from strings

Interesting issue thats caught me out.

I receive string times from a server to a device. which I then convert in to a NSDate. When the device was set to displaying 24hour times, life was good.

Now I am testing it on a device set to 12hour times. everything has stopped working. Dates are coming back as null

I first had

 NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
 [dateFormat setDateFormat:@"HH:mm"];
 self.startTime = [dateFormat dateFromString:(NSString *)self.startTime];

Worked perfectly for devices showing 24hour dates but not 12hour.

I then tried

 NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
 [dateFormat setDateFormat:@"hh:mm"];
 self.startTime = [dateFormat dateFromString:(NSString *)self.startTime];

This works fine up until 12 noon, then all dates are returned as null

Update

I have also tried adding "a" but this still results in returning null

 if (startDate == nil)
 {
      NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
      [dateFormat setDateFormat:@"hh:mm a"];
      startDate = [dateFormat dateFromString:(NSString *)self.startTime];
 }

Update 2

Adding local, adding :ss adding a all still do not work

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *twelveHourLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
dateFormat.locale = twelveHourLocale;
[dateFormat setDateFormat:@"hh:mm a"];
startDate = [dateFormat dateFromString:(NSString *)self.startTime];
like image 647
Burf2000 Avatar asked Aug 15 '13 13:08

Burf2000


3 Answers

It's close... I think you need:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:locale];
[dateFormatter setDateFormat:@"HH:mm"];
startDate = [dateFormatter dateFromString:(NSString *)self.startTime];
like image 76
Ian L Avatar answered Nov 14 '22 14:11

Ian L


You can try this

    12 to 24 hour format


    NSDateFormatter* df = [[NSDateFormatter alloc] init];
    [df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
    [df setTimeZone:[NSTimeZone systemTimeZone]];
    [df setDateFormat:@"hh:mm a"];
    NSDate* newDate = [df dateFromString:LocationTrackingStartTime];
    [df setDateFormat:@"HH:mm"];
    newDate = [df stringFromDate:newDate];

   24 to 12 hour format

    NSDateFormatter* df = [[NSDateFormatter alloc] init];
    [df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
    [df setTimeZone:[NSTimeZone systemTimeZone]];
    [df setDateFormat:@"yyyy-mm-dd hh:mm:ss"];
    NSDate* newDate = [df dateFromString:[df stringFromDate:[NSDate date]]];
    [df setDateFormat:@"hh:mm a"];
    newDate = [df stringFromDate:[NSDate date]];
like image 44
Madhuri Avatar answered Nov 14 '22 14:11

Madhuri


You have to add am/pm:

NSLocale *twelveHourLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
dateFormat.locale = twelveHourLocale;
[dateFormat setDateFormat:@"hh:mm: a"];
like image 4
Abdullah Shafique Avatar answered Nov 14 '22 13:11

Abdullah Shafique