iOS NSDateFormatter 和 NSDate简单使用 时间戳的转换

NSDate :用来表示日期的类,使用时经常要NSDateFormatter(时间管理类)

常用的有下面这些方法
NSDate *date = [NSDate date]; //返回当前时间
dateWithTimeIntervalSinceNow 返回特定时间之后多次时间(秒)的某个时间。
dateWithTimeIntervalSince1970
dateWithTimeInterval: sinceDate:

NSDateFormatter的使用 (需要使用这个类对时间进行格式处理,转化)

 NSDateFormatter *formatter = [[NSDateFormatter alloc] init];  
 // HH是24进制,hh是12进制 (自定义格式) 
 formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";  
 [matter stringFromDate:date];//按照指定格式将时间转成字符串

将时间转换成时间戳(数字型double)

NSTimeInterval a=[date timeIntervalSince1970]*1000; // *1000 是精确到毫秒,不乘就是精确到秒 

将时间戳转换成时间

NSDate *date =[NSDate dateWithTimeIntervalSince1970: 时间戳 (double)] ; //时间戳是10位的,13位的需要/1000 

有的时候需要设置时区为中国时区

 NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值