1

I am getting the rfc3399 formatted date string through google calender web services. I want to convert this string into NSDate object so that I can add an event about this to the local calendar. I found the below method in Apple's documentation regarding NSDateFormatter , but its not working

Date String - 2012-05-23T18:30:00.000-05:00

 - (NSString *)userVisibleDateTimeStringForRFC3339DateTimeString:(NSString *)rfc3339DateTimeString {
/*
 Returns a user-visible date time string that corresponds to the specified
 RFC 3339 date time string. Note that this does not handle all possible
 RFC 3339 date time strings, just one of the most common styles.
 */

NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

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

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

NSString *userVisibleDateTimeString;
if (date != nil) {
    // Convert the date object to a user-visible date string.
    NSDateFormatter *userVisibleDateFormatter = [[NSDateFormatter alloc] init];
    assert(userVisibleDateFormatter != nil);

    [userVisibleDateFormatter setDateStyle:NSDateFormatterShortStyle];
    [userVisibleDateFormatter setTimeStyle:NSDateFormatterShortStyle];

    userVisibleDateTimeString = [userVisibleDateFormatter stringFromDate:date];
}

return userVisibleDateTimeString;
}

Please help me , I have tried changing the date format quite a lot times but no success.

Bharat Jagtap
  • 1,692
  • 2
  • 22
  • 35
  • Simplest thing to do is to pre-process the string to remove the final ":", then use a vanilla format string using "ZZZ" for timezone. However, the new "ZZZZZ" is supposed to work for the style of timestamp containing the ":" -- I've never tried it. – Hot Licks Nov 21 '12 at 04:25
  • 1
    Note that your format string above is missing the "SSS" for milliseconds -- should be something like `"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSSZZZZZ"` – Hot Licks Nov 21 '12 at 04:30

2 Answers2

1

try with this method..

-(NSDate *)convertStringToDate:(NSString *) date {
        NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; 
        NSDate *nowDate = [[[NSDate alloc] init] autorelease];
        [formatter setDateFormat:@"YYYY-MM-DD'T'HH:mm:ssZ"];// set format here which format in string date
        /// also try this format @"yyyy-MM-dd'T'HH:mm:ss.fffK" 
        date = [date stringByReplacingOccurrencesOfString:@"+0000" withString:@""];
        nowDate = [formatter dateFromString:date];
        // NSLog(@"date============================>>>>>>>>>>>>>>> : %@", nowDate);
        return nowDate;
}

call this method like bellow..

NSDate *date = [self convertStringToDate:rfc3339DateTimeString];

also for try with some rfc Date formate with fffK for this format see this bellow. link..

how-do-i-parse-and-convert-datetimes-to-the-rfc-3339-date-time-format

i hope this answer is helpful

Community
  • 1
  • 1
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • Nop , still not working ... when it calls [rfc3339DateFormatter dateFromString:rfc3339DateTimeString] it returns nil . I am not able to get the date object... – Bharat Jagtap Nov 20 '12 at 20:00
  • @BharatJ try this Edited answer with my method and also try other rfc ffk format if required .. i post the link also for ffk.. i hope you'll get solution of this problem mate.. – Paras Joshi Nov 21 '12 at 04:15
0

The issue is the date contains (-05:00 )':' in the timezone value ... you have to remove it from the string then the above method will work fine......The new date string should look like below

2012-05-23T18:30:00.000-0500 - This should be the right format .

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Bharat Jagtap
  • 1,692
  • 2
  • 22
  • 35