Nsdate - Convert Date to Gmt

What is the timezone for [NSDate date]?

An NSDate represents a point in the continuum of time, which is independent of time zones or locale. Looking at that date in a time zone is a "view" of that time (i.e. "what do WE call that time in the temporal continuum?").

If your goal is to show a time in your current time zone, look at NSDateFormatter: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/ - you can set the time-zone (and the locale, if you want) to display in the formatter, and it will show you what that date is referred to as in that time zone / locale.

NSDateFormatter: how to convert date string with 'GMT' to local NSDate?

Note that NSDate's always store the date, internally, in GMT. You then use a date formatter to create a string in your local time zone. In this case, you are starting with a string so need to use the date formatter to create the date in the first place, and then use it again to create a string in your local time zone (which it defaults to when the timezone is not specified).

NSString *dateString           = @"Fri, 30 Nov 2012 00:35:18 GMT";

NSDateFormatter *dateFormatter = [NSDateFormatter new];
dateFormatter.dateFormat = @"EEE, dd MM yyyy HH:mm:ss zzz";

NSDate *date = [dateFormatter dateFromString:dateString];
NSString *localDateString = [dateFormatter stringFromDate:date];

NSLog(@"%@", localDateString);
// Results: Thu, 29 11 2012 19:35:18 EST

Converting UTC date format to local nsdate

Something along the following worked for me in Objective-C :

// create dateFormatter with UTC time format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSDate *date = [dateFormatter dateFromString:@"2015-04-01T11:42:00"]; // create date from string

// change to a readable time format and change to local time zone
[dateFormatter setDateFormat:@"EEE, MMM d, yyyy - h:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:date];

I keep these two websites handy for converting different time formats:
http://www.w3.org/TR/NOTE-datetime

http://benscheirman.com/2010/06/dealing-with-dates-time-zones-in-objective-c/

In Swift it will be:

// create dateFormatter with UTC time format
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone?
let date = dateFormatter.date(from: "2015-04-01T11:42:00")// create date from string

// change to a readable time format and change to local time zone
dateFormatter.dateFormat = "EEE, MMM d, yyyy - h:mm a"
dateFormatter.timeZone = NSTimeZone.local
let timeStamp = dateFormatter.string(from: date!)

Convert GMT NSDate to device's current Time Zone

NSDate is always represented in GMT. It's just how you represent it that may change.

If you want to print the date to label.text, then convert it to a string using NSDateFormatter and [NSTimeZone localTimeZone], as follows:

NSString *gmtDateString = @"08/12/2013 21:01";

NSDateFormatter *df = [NSDateFormatter new];
[df setDateFormat:@"dd/MM/yyyy HH:mm"];

//Create the date assuming the given string is in GMT
df.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *date = [df dateFromString:gmtDateString];

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

// My local timezone is: Europe/London (GMT+01:00) offset 3600 (Daylight)
// prints out: date = 08/12/2013 22:01

Convert NSDate to String with a specific timezone in SWIFT

Xcode 8 beta • Swift 3.0

Your ISO8601 date format is basic hms without Z. You need to use "xxxx" for "+0000".

"+0000" means UTC time.

let dateString = "2015-09-04 22:15:54 +0000"

let dateFormatter = DateFormatter()
dateFormatter.calendar = Calendar(calendarIdentifier: .ISO8601)
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss xxxx"
dateFormatter.locale = Locale(localeIdentifier: "en_US_POSIX")
if let dateToBeSaved = dateFormatter.date(from: dateString) {
print(dateToBeSaved) // "2015-09-04 22:15:54 +0000"
}

If you need some reference to create your date format you can use this:

Sample Image

Objective-C setting NSDate to current UTC

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

How to convert a string UTC date to NSDate in Swift

I think this should work

let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let localDate = formatter.date(from: date)

Make NSDateFormatter emit the string UTC instead of the string GMT ?

There is no date format specifier that returns UTC.

Since you are hardcoding the UTC timezone for the formatter, then simply hardcode the string UTC in the date format.

Also note that you want yyyy, not YYYY for the year.

[dateFormatter setDateFormat:@"yyyy-MM-dd--HH-mm-ss 'UTC'"];

If you need to handle other timezones and want GMT to appear as UTC, then use zzz (as you already are) and use string replacement on the result to convert GMT to UTC.

One other possible idea to consider is to use some number of X for the timezone specifier. This will give the timezone offset as numbers but if the timezone offset is 0, then it results in Z (for Zulu).



Related Topics



Leave a reply



Submit