1
votes

I've already searched StackOverflow.com for an answer to this question, still without any success. Already looked here:

iOS - Converting time and date to user time zone

Date Format - time zone

Get current iPhone device timezone date and time from UTC-5 timezone date and time iPhone app?

IOS how to set date format

Nothing of those worked.


So I have this NSString date format: 2014-05-07T10:28:52.000Z trying to convert it to NSDate, this is what I'm using:

+ (NSDate*)stringToDate:(NSString*)string format:(NSString*)format
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    return [dateFormatter dateFromString:string];
}

This is how I'm using it:

NSDate *date = [AppUtils stringToDate:youtube.postDate format:@"yyyy-MM-dd'T'HH:mm:ssZZZ"];

I've also tried those formats:

yyyy-MM-dd'T'HH:mm:ss.ZZZ
yyyy-MM-dd'T'HH:mm.ss.ZZZ
yyyy-MM-dd'T'HH:mm.ssZZZ

How could I convert it to NSDate successfully?

Thanks in advance!

2
What is the current result ? and have you an exemple of what can be youtube.postDate ? - KIDdAe
You have forgotten to tell us what happens. Also you quote other stackoverflow questions and mention they don't help, and yet your code doesn't even set a time zone, so I don't think you've actually read/understood those other questions. - trojanfoe
Shouldn't that be ss.ZZZ? - trojanfoe
I did read and tried other options, but none of them worked for me. Also this is not the first time I'm using NSDateFormatter and I still have no clue why it doesn't work. - Yossi Tsafar
@YossiTsafar : see my answer is working.. Edit 3 with ur case... Edit 2 with my case... - Fahim Parkar

2 Answers

2
votes

If the date format is fixed, what I do is below.

Replace T by space
Replace Z by blank 

And then do formatting...

NSString *dateReceivedInString = @"2014-05-07T10:28:52.000Z";
dateReceivedInString = [dateReceivedInString stringByReplacingOccurrencesOfString:@"T" withString:@" "];
dateReceivedInString = [dateReceivedInString stringByReplacingOccurrencesOfString:@"Z" withString:@""];

Now do the formatting using

yyyy-MM-dd HH:mm:ss

Edit 1

If you want to work with your case, use below

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

Edit 2

I tried this and it is working.

NSString *dateReceivedInString = @"2014-05-07T10:28:52.000Z";
dateReceivedInString = [dateReceivedInString stringByReplacingOccurrencesOfString:@"T" withString:@" "];
dateReceivedInString = [dateReceivedInString stringByReplacingOccurrencesOfString:@"Z" withString:@""];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
NSLog(@"ddddd====%@", [dateFormatter dateFromString:dateReceivedInString]);

Edit 3

To make working with your case use below.

NSString *dateReceivedInString = @"2014-05-07T10:28:52.000Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSLog(@"ddddd====%@", [dateFormatter dateFromString:dateReceivedInString]);
0
votes

Here are 2 methods that I use to convert RFC3339 Date string to NSDate,
And also Method for converting NSDate to RFC3339 Date string

Method for converting RFC3339 Date string to NSDate

+ (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 an NSDate.
    return [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];}

Method for converting NSDate to RFC3339 Date string

+ (NSString *)RFC3339DateTimeFromDate:(NSDate *)aDate
{
    NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];

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

    return [rfc3339DateFormatter stringFromDate:aDate];
}