1. NSDate
Apple iOS 中的日期类型, 它本身是没有显示的格式的, 在界面上展示的时候需要借助其他类型变量.
常用实例化方法:
- +date
获取当前时间的date - +dateWithTimeIntervalSinceNow:
传入一个以秒为单位的时间间隔interval获取到从现在开始interval秒后的一个date - +dateWithTimeInterval:sinceDate:
传入一个以秒为单位的时间间隔interval和一个date获取到从date开始interval秒后的一个date - +dateWithTimeIntervalSinceReferenceDate:
传入一个以秒为单位的时间间隔interval获取到从2001年1月1日0时0分0秒UTC(世界标准时间)开始interval秒后的一个date - +dateWithTimeIntervalSince1970:
传入一个以秒为单位的时间间隔interval获取到从1970年1月1日0时0分0秒UTC(世界标准时间)开始interval秒后的一个date
Apple官方文档说明如下
+ date
Creates and returns a new date set to the current date and time.
+ dateWithTimeIntervalSinceNow:
Creates and returns an NSDate object set to a given number of seconds from the current date and time.
+ dateWithTimeInterval:sinceDate:
Creates and returns an NSDate object set to a given number of seconds from the specified date.
+ dateWithTimeIntervalSinceReferenceDate:
Creates and returns an NSDate object set to a given number of seconds from 00:00:00 UTC on 1 January 2001.
+ dateWithTimeIntervalSince1970:
Creates and returns an NSDate object set to the given number of seconds from 00:00:00 UTC on 1 January 1970.
2. NSTimeInterval
Apple官方文档说明如下
它是用来实例化一个时间间隔, 单位是秒.
数据类型是double
它只可以以秒为单位被实例化, 它拥有10, 000年时间范围内的亚毫秒级的精度.
3. NSDateFormatter
时间显示格式对象
可通过对象方法 -setDateFormat: 设置格式.
例如:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
获取当前时间:
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
NSString *DateTime = [formatter stringFromDate:date];
NSLog(@"%@",DateTime);