JSON date to NSDate Issue

I have a date string in JSON format, it looks like this: 2013-05-22T10:54:40.437

And I'm trying to convert it to NSDate.

- (NSString *)dateFromString:(NSString *)datestring{
    // Convert string to date object
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss"];

    NSDate *date = [dateFormat dateFromString:datestring];
    //date is nil here.
    NSDateFormatter *newDateFormatter = [[NSDateFormatter alloc]init];
    [newDateFormatter setDateFormat:@"MM/dd/yyyy"];
    NSString *newString = [newDateFormatter stringFromDate:date];
    NSLog(@"Date -- %@",datestring);


    return newString;
}

But date is nil after dateFromString. Does anyone know what I did wrong here? Thanks!

Answers


Use this date format:

yyyy-MM-dd'T'HH:mm:ss.SSS

Your code with little modification:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"];

NSDate *date = [dateFormat dateFromString:datestring];
NSDateFormatter *newDateFormatter = [[NSDateFormatter alloc]init];
[newDateFormatter setDateFormat:@"MM/dd/yyyy"];
NSString *newString = [newDateFormatter stringFromDate:date];
NSLog(@"Date: %@, formatted date: %@", date, newString);

gives me this output:

Date: 2013-05-22 08:54:40 +0000, formatted date: 05/22/2013

Edit: how "ikinci viking" pointed out in comment, escaping the dashes is unnecessary. Escaping removed from the code


Update for iOS 11+

ISO8601DateFormatter is the recommended way to parse internet date on iOS 10+.

However it has few issues preventing it from usage with the specific format from the question:

  • Milliseconds are supported only on iOS 11+
  • It does not work for dates without timezone (the case in question). Date string must contain timezone part (e.g. +00:00 or Z for "Zulu time" UTC)

If date strings contains both milliseconds and timezone (e.g. 2018-09-07T14:04:13.143Z), it can be used as follows (Swift example):

let isoDateFormatter = ISO8601DateFormatter()
isoDateFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]

Your date format is incorrect. It should be:

[dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss:SSS"];

The three capital 'S' means it will take the fractional part to 3 digits. Adjust it as you will.


If your date format is "2014-02-25T13:05:10+00:00" then,
your code should be like this....

    NSURL *url = [NSURL URLWithString:@"your URL"];
    NSString *str = [[NSString alloc] initWithContentsOfURL:url usedEncoding:Nil error:Nil];

    NSDateFormatter *dateFor = [[NSDateFormatter alloc] init];
    [dateFor setDateFormat:**@"yyyy-MM-dd'T'HH:mm:ss'+'SS:SS"**];

    NSDate *yourDate = [dateFor dateFromString:str];
    NSLog(@"Your Date: %@",yourDate);

-(void)viewDidLoad {

    self.date = [dateForRFC3339DateTimeString:@"2015-01-18T01:00:00.000Z"];

}


-(NSDate *)dateForRFC3339DateTimeString:(NSString *)rfc3339DateTimeString {

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

    [rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'"];
    [rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    // Convert the RFC 3339 date time string to a NSDate.
    NSDate *result = [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];
    return result;
} 

Need Your Help

Extension methods on a struct

c# .net struct extension-methods

Can you add extension methods to a struct?

Play Framework 2.1 - AngularJS routing - best solution?

scala angularjs playframework

I am working my way through the AngularJS tutorial. Angular uses it's own JS routing mechanism to allow for single page apps. A sample routing file for Angular looks like this: