Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Init NSDate with a stringDate without timezone calculations

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

like image 521
Hassy Avatar asked Sep 08 '25 02:09

Hassy


1 Answers

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"
}
like image 185
Leo Dabus Avatar answered Sep 10 '25 09:09

Leo Dabus