美文网首页
关于时间戳

关于时间戳

作者: 不知道为什么 | 来源:发表于2018-02-02 10:20 被阅读36次

    第一次写简书有点小激动哈,关于时间戳这个我一直很混乱用的时候就胡乱粘贴完成需求,现在有时间了整理下。NSDate*datenow = [NSDatedate];

    NSLog(@"datenow = %@",datenow);

    文档如下

    Creates and returns a new date set to the current date and time.

    A new date object set to the current date and time.

    Returns A new date object set to the current date and time.

    大概意思是说

    创建并返回一个新的日期设置为当前日期和时间。

    设置为当前日期和时间,一个新的Date对象。

    返回设置为当前日期和时间,一个新的Date对象。

    首先初始化NSDate  并且打印了他结果是这样的

    结果他与本地时间相差 8个小时  原因是好像是从ios4.1开始[NSDate date]返回的是GMT,GMT是中央时区,北京在东8区,相差8个小时(原谅我地理知识全还给老师了)那怎么办呢,接下来看

    NSTimeZone*zone = [NSTimeZonesystemTimeZone];

    NSLog(@"systemTimeZone = %@",zone);

    文档如下

    Returns the time zone currently used by the system.

    The time zone currently used by the system. If the current time zone cannot be determined, returns the GMT time zone.

    If you get the system time zone, it is cached by the application and does not change if the user subsequently changes the system time zone. The next time you invoke systemTimeZone, you get back the same time zone you originally got. You have to invoke resetSystemTimeZone to clear the cached object.

    大概意思:

    NSTimeZone 返回的是系统设定好的时区 如果无法确定时区那么久返回中央时区。而且这个时区是被缓存起来的,就算获取后用户改变了时区,系统还是获取第一次获取的时区,否则你必须调用 调用resetSystemTimeZone清除缓存的对象。(文档是好东西)。

    然后我就实验下 时区设置成英国蛋疼的事情发生了  我没调用 resetSystemTimeZone 他还是更改了时区 这文档毛意思!求大神们指正 resetSystemTimeZone 有毛用啊!

    NSInteger interval = [zone secondsFromGMTForDate:datenow];

    本地时区 与中央时区的时间差

    NSDate *localeDate = [datenow  dateByAddingTimeInterval: interval];

    通过计算这个时间差就是本地区现在的时间了

    NSLog(@"%@", localeDate);

    相关文章

      网友评论

          本文标题:关于时间戳

          本文链接:https://www.haomeiwen.com/subject/iuralttx.html