美文网首页
NSDate NSDateFormatter 时间戳

NSDate NSDateFormatter 时间戳

作者: 超_iOS | 来源:发表于2018-01-03 18:25 被阅读116次
    NSDate *date = [NSDate date];//零时区的时间(格林尼治)
    本机的时间 减去 本机时区的固定差值算出的UTC时间。所以手动改手机时间的话,结果也会变
    
    //      NSDate *nowDate = [[NSDate alloc] initWithTimeIntervalSinceNow:28800];//北京时间
            
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Shanghai"]];东八区
            
            NSString *nowDateStr = [dateFormatter stringFromDate:nowDate];
            NSString *dateStr = [dateFormatter stringFromDate:date];////北京时间字符串
    

    yyyy-MM-dd HH:mm:ss:使用24小时制格式化日期
    yyyy-MM-dd hh:mm:ss:使用12小时制格式化日期

    使用NSDateFormatter转换时间字符串时,默认的时区是系统时区,例如在中国一般都是北京时间(+8),如果直接转换会导致结果相差8小时,所以一般的做法是先指定时区为GMT标准时间再转换,

    时间戳分时区吗:

    image.png

    相关文章

      网友评论

          本文标题:NSDate NSDateFormatter 时间戳

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