RahulMishra RahulMishra - 3 months ago 9
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
po
local Date I get this:

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


When I use DateFormatter to Log
localDate


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
2016-09-06-23-00-00


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

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);

Output:

formattedDate: Tue Sep 06 2016 17:30:00