RahulMishra RahulMishra - 1 year ago 61
iOS Question

Issue while converting timeStamp to local Time Zone Date

I have a timestamp generated from Here

//Timestamp 1473163200000

I want to convert it inot Date Object based on Current Time Zone.

I am doing this as:

NSDate *gmtDate = [NSDate dateWithTimeIntervalSince1970:1473163200000/1000];
NSTimeInterval timeZoneOffset = [[NSTimeZone systemTimeZone] secondsFromGMTForDate:gmtDate];

NSDate *localDate = [gmtDate dateByAddingTimeInterval:timeZoneOffset];

Now when I
local Date I get this:

(lldb) po localDate
2016-09-06 17:30:00 +0000

When I use DateFormatter to Log

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd-HH-mm-ss"];
NSString *newDate = [dateFormat stringFromDate:localDate];

I am getting this:

(lldb) po newDate

But I want this to be:

Tue Sep 06 2016 17:30:00

What I am missing ?
Please avoid formatting i am more curious about time zone.

Answer Source

Try this code

NSDate *date = [NSDate dateWithTimeIntervalSince1970:1473163200000/1000];

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];

dateFormatter.dateFormat = @"EEE MMM dd yyyy HH:mm:ss";
NSString *formattedDate = [dateFormatter stringFromDate: date];

NSLog(@"formattedDate: %@",formattedDate);


formattedDate: Tue Sep 06 2016 17:30:00
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download