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];

本文详细介绍了NSDate类及其常用方法,包括如何获取当前时间、计算特定时间间隔等。同时深入探讨了NSDateFormatter类的使用方法,例如如何设置时间格式、时区及实现时间与字符串之间的转换。
798

被折叠的 条评论
为什么被折叠?



