使用Objective c将时代时间转换为带有好时区的NSDate [英] Convert epoch time to NSDate with good timezone with Objective c

查看:356
本文介绍了使用Objective c将时代时间转换为带有好时区的NSDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将一个纪元时间值转换为NSDate。例如,我使用这个值:1310412600000.我在EDT时区。



当我尝试这样:

  NSString * bar = [[NSDate dateWithTimeIntervalSince1970:epoch] description]; 

我有一个错误的值...



什么是好的方法?


解决方案

纪元时间(也称为Unix和POSIX时间)是从1970年开始的秒数。您可以使用此算法将纪元时间转换为NSDate:


  1. 实例化NSDate对象,并将纪元时间作为参数传递给NSDate的initWithTimeIntervalSince1970初始值设定器。 完成。 NSDate对象设置为纪元时间。 NSDate在UTC时区内部存储时间。

  2. [可选]使用NSDateFormatter格式化NSDate到相应的时区。

这里是我使用的代码:

  //示例字符串epochTime是自1970年以来的秒数
NSString * epochTime = @1316461149;

//将NSString转换为NSTimeInterval
NSTimeInterval seconds = [epochTime doubleValue];

//(步骤1)创建NSDate对象
NSDate * epochNSDate = [[NSDate alloc] initWithTimeIntervalSince1970:seconds];
NSLog(@Epoch time%@等于UTC%@,epochTime,epochNSDate);

//(第2步)使用NSDateFormatter在本地时区显示epochNSDate
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@yyyy-MM-dd HH:mm:ss zzz];
NSLog(@Epoch time%@等于%@,epochTime,[dateFormatter stringFromDate:epochNSDate]);

//(Just for interest)显示当前时区
NSString * currentTimeZone = [[dateFormatter timeZone] abbreviation];
NSLog(@(您的本地时区是:%@),currentTimeZone);


how I can convert an epoch time value to NSDate. For example I use this value : 1310412600000. and I am in the EDT time zone.

When I try this :

NSString *bar = [[NSDate dateWithTimeIntervalSince1970:epoch] description];

I got a wrong value...

What is the good way? I spend a lot of time with that....

Thanks

解决方案

Epoch time (also known as "Unix" and "POSIX" time) is the number of seconds since the start of 1970. You can convert epoch time to NSDate with this algorithm:

  1. Instantiate NSDate object and pass the epoch time as a parameter to NSDate's initWithTimeIntervalSince1970 initializer. You're done. The NSDate object is set to the epoch time. NSDate stores times internally in the UTC time zone. It's up to you how it is displayed.
  2. [Optional] Format your NSDate to the appropriate time zone with an NSDateFormatter.

Here's the code I used:

    // Sample string epochTime is number of seconds since 1970
NSString *epochTime = @"1316461149";

// Convert NSString to NSTimeInterval
NSTimeInterval seconds = [epochTime doubleValue];

// (Step 1) Create NSDate object
NSDate *epochNSDate = [[NSDate alloc] initWithTimeIntervalSince1970:seconds];
NSLog (@"Epoch time %@ equates to UTC %@", epochTime, epochNSDate);

// (Step 2) Use NSDateFormatter to display epochNSDate in local time zone
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];
NSLog (@"Epoch time %@ equates to %@", epochTime, [dateFormatter stringFromDate:epochNSDate]);

// (Just for interest) Display your current time zone
NSString *currentTimeZone = [[dateFormatter timeZone] abbreviation];
NSLog (@"(Your local time zone is: %@)", currentTimeZone);

这篇关于使用Objective c将时代时间转换为带有好时区的NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆