0

I am trying to convert NSDate to UTC Date and this is what I am using..

NSString* dateString= [NSDate utcStringFromDate:[NSDate date] withFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSZ"];

+ (NSString *)utcStringFromDate:(NSDate *)date withFormat:(NSString *)format{
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [outputFormatter setDateFormat:format];
    NSString *timestamp_str = [outputFormatter stringFromDate:date];
    [outputFormatter release];
    return timestamp_str;
}

This works fine most of the times and gives me a result similar to this.

2013-05-03T05:30:25.18+0000

but at times it gives me a strange value like

2013-04-26T12:03:53 a.m..24+0000

Now I am not sure why it does that..

Thanks for any help.

Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115

0 Answers0