简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM