1
votes

I am getting a date time from web service which is in UTC format. Now i want to assign it to NSDate. Following is the code i have done

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];

NSDate *utcDate = [dateFormatter dateFromString:@"2015-08-18T23:00:00"];

but it is calculating its timezone calculations by default the result is 2015-08-19 03:00:00 +0000. How can i initialize NSDate with same date and Time. I want to perform timezone calculations later on

3
Uncomment that timezone line and set it to UTC. - Desdenova

3 Answers

6
votes

edit/update:

Xcode 11 • Swift 5.1:

let dateString = "2015-08-18T23:00:00"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
if let dateFromString = formatter.date(from: dateString) {
    print(dateFromString)   // "2015-08-18 23:00:00 +0000"
}
1
votes

A default-allocated NSDateFormatter will use the current locale (the one that the user set up in Settings.app).

You have to explicitly set a suitable locale on the formatter:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

This locale uses the gregorian calendar on UTC without any DST adjustments.

Edit: LeoDabus points out that setting the locale does not change the timezone of the date formatter. This has to be done explicitly:

dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
-2
votes

NSString *dateStr =[NSString stringWithFormat:@"%@",[dict valueForKey:@"utc_datetime"]];

NSString* input = dateStr;
NSString* format = @"dd MMM yyyy - HH:mm:ss'";

// Set up an NSDateFormatter for UTC time zone
NSDateFormatter* formatterUtc = [[NSDateFormatter alloc] init];
[formatterUtc setDateFormat:format];
[formatterUtc setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

// Cast the input string to NSDate
NSDate* utcDate = [formatterUtc dateFromString:input];

// Set up an NSDateFormatter for the device's local time zone
NSDateFormatter* formatterLocal = [[NSDateFormatter alloc] init];
[formatterLocal setDateFormat:format];
[formatterLocal setTimeZone:[NSTimeZone localTimeZone]];

// Create local NSDate with time zone difference
NSDate* localDate = [formatterUtc dateFromString:[formatterLocal stringFromDate:utcDate]];
NSLog(@"utc:   %@", utcDate);
NSLog(@"local: %@", localDate);

NSDateFormatter *format1q = [[NSDateFormatter alloc] init];
format1q.dateFormat = @"HH:mm";


NSString *StrDAte=[NSString stringWithFormat:@"%@",[format1q stringFromDate:utcDate]];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"HH:mm";
NSDate *date = [dateFormatter dateFromString:StrDAte];
date = [date dateByAddingTimeInterval:-60];

dateFormatter.dateFormat = @"hh:mm a";
NSString *pmamDateString = [dateFormatter stringFromDate:date];