Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS : Switching between 12/24 hour times from strings
    primarykey
    data
    text
    <p>Interesting issue thats caught me out.</p> <p>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.</p> <p>Now I am testing it on a device set to 12hour times. everything has stopped working. Dates are coming back as null</p> <p>I first had</p> <pre><code> NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"HH:mm"]; self.startTime = [dateFormat dateFromString:(NSString *)self.startTime]; </code></pre> <p>Worked perfectly for devices showing 24hour dates but not 12hour.</p> <p>I then tried </p> <pre><code> NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"hh:mm"]; self.startTime = [dateFormat dateFromString:(NSString *)self.startTime]; </code></pre> <p>This works fine up until 12 noon, then all dates are returned as null</p> <p><strong>Update</strong></p> <p>I have also tried adding "a" but this still results in returning null</p> <pre><code> if (startDate == nil) { NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"hh:mm a"]; startDate = [dateFormat dateFromString:(NSString *)self.startTime]; } </code></pre> <p><strong>Update 2</strong></p> <p>Adding local, adding :ss adding a all still do not work</p> <pre><code>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]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload