zoukankan      html  css  js  c++  java
  • iOS标准时间与时间戳相互转换

     1 NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
     2     [formatter setDateStyle:NSDateFormatterMediumStyle];
     3     [formatter setTimeStyle:NSDateFormatterShortStyle];
     4     [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
     5     
     6     //设置时区,这个对于时间的处理有时很重要
     7     //例如你在国内发布信息,用户在国外的另一个时区,你想让用户看到正确的发布时间就得注意时区设置,时间的换算.
     8     //例如你发布的时间为2010-01-26 17:40:50,那么在英国爱尔兰那边用户看到的时间应该是多少呢?
     9     //他们与我们有7个小时的时差,所以他们那还没到这个时间呢...那就是把未来的事做了
    10     
    11     NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    12     [formatter setTimeZone:timeZone];
    13     NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    14     NSString *nowtimeStr = [formatter stringFromDate:datenow];//----------将nsdate按formatter格式转成nsstring
    15     
    16     NSDate *dateNow2 = [formatter dateFromString:nowtimeStr];
    17     
    18 //    时间转时间戳的方法:
    19 //    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];//这两个效果一样
    20     NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[dateNow2 timeIntervalSince1970]];//这两个效果一样
    21     NSLog(@"timeSp:%@",timeSp); //时间戳的值
    22 //    时间戳转时间的方法
    23 //    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:1296035591];
    24 //    NSLog(@"1296035591  = %@",confromTimesp);
    25 //    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    26 //    NSLog(@"confromTimespStr =  %@",confromTimespStr);
  • 相关阅读:
    SqlServer注意事项总结,高级程序员必背!
    C#语法——委托,架构的血液
    C#语法——泛型的多种应用
    C#——Nhibernate探索
    C#语法——await与async的正确打开方式
    谈谈我理解的SA——Systems Architecture
    C#线程安全使用(五)
    C#语法——元组类型
    架构师的御人之道
    另一个角度的架构师
  • 原文地址:https://www.cnblogs.com/codemakerhj/p/5717523.html
Copyright © 2011-2022 走看看