Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to format UTC date with NSDateFormatter

I have the following date:

2011-10-20T01:10:50Z

I would like it to be formatted to

"M/d/yy 'at' h:mma"

Here is my code:

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    //The Z at the end of your string represents Zulu which is UTC
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [dateFormatter setDateFormat:@"yyyy-dd-MM'T'HH:mm:ss'Z'"];
    NSDate* newTime = [dateFormatter dateFromString:[message valueForKey:@"created_at"]];

    //Add the following line to display the time in the local time zone
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    [dateFormatter setDateFormat:@"M/d/yy 'at' h:mma"];
    NSString* finalTime = [dateFormatter stringFromDate:newTime];
    [dateFormatter release];
    NSLog(@"%@", finalTime);    

Unfortunately the finalTime is NULL here.

like image 588
Sheehan Alam Avatar asked Oct 20 '11 01:10

Sheehan Alam


People also ask

Is NSDateFormatter thread safe?

Thread SafetyOn iOS 7 and later NSDateFormatter is thread safe. In macOS 10.9 and later NSDateFormatter is thread safe so long as you are using the modern behavior in a 64-bit app.

How do you convert UTC to local time in Swift 5?

var isTimeFromServer = true var time:String! var period:String! let timeString = "6:59 AM" //Current UTC time if isTimeFromServer { let index = timeString. index(timeString. startIndex, offsetBy: 5) let twelve = timeString.

What is Dateformatter locale?

dateFormat(fromTemplate:options:locale:)Returns a localized date format string representing the given date format components arranged appropriately for the specified locale. iOS 4.0+ iPadOS 4.0+ macOS 10.6+ Mac Catalyst 13.1+ tvOS 9.0+ watchOS 2.0+

What is en_US_POSIX?

"en_US_POSIX" is also invariant in time (if the US, at some point in the future, changes the way it formats dates, "en_US" will change to reflect the new behaviour, but "en_US_POSIX" will not), and between machines ("en_US_POSIX" works the same on iOS as it does on OS X, and as it it does on other platforms).


1 Answers

You have dd-MM backwards. You have 10 for dd and 20 MM. There is no month 20.

2011-10-20T01:10:50Z
@"yyyy-dd-MM'T'HH:mm:ss'Z'"

Because of that, the newTime was null and the finalTime was null.

Here's with the fix. This:

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];

//The Z at the end of your string represents Zulu which is UTC
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];

NSDate* newTime = [dateFormatter dateFromString:@"2011-10-20T01:10:50Z"];
NSLog(@"original time: %@", newTime);

//Add the following line to display the time in the local time zone
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:@"M/d/yy 'at' h:mma"];
NSString* finalTime = [dateFormatter stringFromDate:newTime];
NSLog(@"%@", finalTime);    

[dateFormatter release];

Outputs:

2011-10-19 22:05:15.107 Craplet[4231:707] original time: 2011-10-20 01:10:50 +0000
2011-10-19 22:05:15.116 Craplet[4231:707] 10/19/11 at 9:10PM
like image 187
bryanmac Avatar answered Sep 30 '22 05:09

bryanmac