Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C setting NSDate to current UTC

Is there an easy way to init an NSDate with the current UTC date/time?

like image 766
Brodie Avatar asked Apr 11 '10 02:04

Brodie


People also ask

How do I get current timeZone in Objective C?

timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0]; //Create a date string in the local timezone dateFormatter. timeZone = [NSTimeZone timeZoneForSecondsFromGMT:[NSTimeZone localTimeZone]. secondsFromGMT]; NSString *localDateString = [dateFormatter stringFromDate:[NSDate date]]; NSLog(@"date = %@", localDateString);

How do I get today's date in Objective C?

In this article i'll show you how to do some common tasks with the NSDate object. Get Today's Date: NSDate* date = [NSDate date];

How do I get current UTC time in Swift?

Use DateFormatter to parse and format a date in Swift. The default time zone for a DateFormatter is the device's local time, so to obtain UTC time the timeZone value needs to be configured.

What is NSDate object?

NSDate objects encapsulate a single point in time, independent of any particular calendrical system or time zone. Date objects are immutable, representing an invariant time interval relative to an absolute reference date (00:00:00 UTC on 1 January 2001).


2 Answers

[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; } 
like image 198
jessecurry Avatar answered Sep 19 '22 03:09

jessecurry


NSDate is a reference to an interval from an absolute reference date, January 1, 2001 00:00 GMT. So the class method [NSDate date] 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.

like image 22
rcw3 Avatar answered Sep 20 '22 03:09

rcw3