Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change NSDate from one time zone to another [closed]

Given a date in NSString like "2012-12-17 04:36:25" (which is GMT) how one can simply change it to other time zones like EST, CST

All the steps I saw so far took so many unnecessary steps

like image 356
shebelaw Avatar asked Dec 17 '12 16:12

shebelaw


People also ask

How to convert date time from one time zone to another?

Here is another formula also can help you: =A2 + TIME (16,0,0) or = A2 - TIME (16,0,0), with this formula, you can return the date and time result directly. You can easily convert date time from one time zone to another with Add hours to date function of Kutools for Excel. Please do as follows.

How to convert localdatetime to zoneddatetime?

ZonedDateTime zonedGmt = localDateTime.atZone(ZoneId.of("GMT")); Once again, the object lives up to its name. It's converting the LocalDateTime object to a ZonedDateTime object which means it represents a point in time in a specific time zone.

What are the parameters of the date and time conversion method?

This method's parameters are the date and time value to convert, the identifier of the date and time value's time zone, and the identifier of the time zone to convert the date and time value to.

Why do I need to change the date time in reports?

Supposing you are working in a foreign company, and you receive lots of reports with different date times from different time zone every day. You need to change those date time from the senders time zones to your current time zone in order to combine all report data together.


1 Answers

NSString *str = @"2012-12-17 04:36:25";
NSDateFormatter* gmtDf = [[[NSDateFormatter alloc] init] autorelease];
[gmtDf setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
[gmtDf setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate* gmtDate = [gmtDf dateFromString:str];
NSLog(@"%@",gmtDate);

NSDateFormatter* estDf = [[[NSDateFormatter alloc] init] autorelease];
[estDf setTimeZone:[NSTimeZone timeZoneWithName:@"EST"]];
[estDf setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *estDate = [estDf dateFromString:[gmtDf stringFromDate:gmtDate]]; // you can also use str
NSLog(@"%@",estDate);

Edit : Adding swift code

let str: String = "2012-12-17 04:36:25"
let gmtDf: NSDateFormatter = NSDateFormatter()
gmtDf.timeZone = NSTimeZone(name: "GMT")
gmtDf.dateFormat = "yyyy-MM-dd HH:mm:ss"
let gmtDate: NSDate = gmtDf.dateFromString(str)!
print(gmtDate)
let estDf: NSDateFormatter = NSDateFormatter()
estDf.timeZone = NSTimeZone(name: "EST")
estDf.dateFormat = "yyyy-MM-dd HH:mm:ss"
let estDate: NSDate = estDf.dateFromString(gmtDf.stringFromDate(gmtDate))!
print(estDate)

Edit: Adding Swift 3 code

    let str: String = "2012-12-17 04:36:25"
    let gmtDf = DateFormatter()
    gmtDf.timeZone = TimeZone(identifier: "GMT")
    gmtDf.dateFormat = "yyyy-MM-dd HH:mm:ss"
    let gmtDate = gmtDf.date(from: str)!
    print(gmtDate)

    let estDf = DateFormatter()
    estDf.timeZone = TimeZone(identifier: "EST")
    estDf.dateFormat = "yyyy-MM-dd HH:mm:ss"
    let estDate = estDf.date(from: gmtDf.string(from: gmtDate))!
    print(estDate)
like image 135
Inder Kumar Rathore Avatar answered Oct 18 '22 20:10

Inder Kumar Rathore