is there an easy way to init an NSDate with the current UTC date/time?
+6
A:
[NSDate date];
You may want to create a category that does something like this:
-(NSString *)getUTCFormateDate:(NSDate *)localDate
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:localDate];
[dateFormatter release];
return dateString;
}
jessecurry
2010-04-11 02:26:15
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];[dateFormatter setTimeZone:timeZone];that nailed it thanks
Brodie
2010-04-11 02:46:59
don't forget to release the date formatter...
Dave DeLong
2010-10-21 19:42:14
if this is moving into production you may actually want to create a static dateFormatter that is lazy initialized. Creation of a dateFormatter is actually somewhat expensive.
jessecurry
2010-10-21 20:52:13
+1
A:
NSDate is a reference to an interval from an absolute reference date, January 1, 2001 00:00 GMT. So the class method [NSDate now] will return a representation of that interval. To present that data in a textual format in UTC, just use the NSDateFormatter with the appropriate NSTimeZone (UTC) to render as needed.
rcw3
2010-04-11 02:44:12
A:
You want an NSDate set to UTC. (Not sure why everyone is giving you "local date" or "NSString" values.)
Patricia
2010-10-21 19:36:37