1

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!

Community
  • 1
  • 1
ytpm
  • 4,962
  • 6
  • 56
  • 113
  • What is the current result ? and have you an exemple of what can be `youtube.postDate` ? – KIDdAe Jul 07 '14 at 07:45
  • 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 Jul 07 '14 at 07:46
  • 1
    Shouldn't that be `ss.ZZZ`? – trojanfoe Jul 07 '14 at 07:47
  • 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. – ytpm Jul 07 '14 at 07:48
  • if the date format is fixed what I do is replace `T` by `space` and replace `Z` by `blank` and do formatting... – Fahim Parkar Jul 07 '14 at 07:49
  • I've edited my question and added some other formats that I've used. Actually I've tried almost every combination there is - still not working. – ytpm Jul 07 '14 at 07:50
  • @KIDdAe I've written in the question what the result should be `2014-05-07T10:28:52.000Z ` - this is the result - always with this format. – ytpm Jul 07 '14 at 07:52
  • 1
    @YossiTsafar : see my answer is working.. Edit 3 with ur case... Edit 2 with my case... – Fahim Parkar Jul 07 '14 at 08:08

2 Answers2

2

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]);
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
0

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];
}

l0gg3r
  • 8,864
  • 3
  • 26
  • 46