OC语言中NSDate的用法
//获取当前系统时间 标准时间 GMT 格林尼治时间
NSDate *date = [NSDate date];
NSLog(@"date is %@", date);
NSDate *date1 = [[NSDate alloc] init];
NSLog(@"date1 is %@", date1);
//获取时间戳
NSTimeInterval time1970 = [date timeIntervalSince1970];
NSLog(@"time1970 is %.2f", time1970);
//到当前世纪(2001)的时间
NSTimeInterval time2001 = [date timeIntervalSinceReferenceDate];
NSLog(@"time2001 is %.2f", time2001);
//到当前的时间
NSTimeInterval time = [date timeIntervalSinceNow];
NSLog(@"time is %.2f", time);
//获取昨天的时间
NSTimeInterval second = 24 * 60 * 60;
NSDate *yesterDayDate = [[NSDate alloc] initWithTimeIntervalSinceNow:-second];
NSLog(@"yesterDayDate is %@", yesterDayDate);
//获取明天的时间
NSDate *tommorrowDate = [NSDate dateWithTimeInterval:second sinceDate:[NSDate date]];
NSLog(@"tommorrowDate is %@", tommorrowDate);
//获取将来的时间(最大值)
NSDate *future = [NSDate distantFuture];
NSLog(@"future is %@", future);
//获取古代的时间
NSDate *past = [NSDate distantPast];
NSLog(@"past is %@", past);
//日期的比较
// BOOL isTure = [date isEqualToDate:date];
// NSLog(@"isTure is %d", isTure);
//返回两个时间比较早的那个时间
NSDate *earlierDate = [tommorrowDate earlierDate:yesterDayDate];
NSLog(@"earlierDate is %@", earlierDate);
//返回两个时间比较晚的那个时间
NSDate *laterDate = [tommorrowDate laterDate:yesterDayDate];
NSLog(@"laterDate is %@", laterDate);
//将时间戳转换成日期
NSTimeInterval second1 = 123456789;
NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:second1];
NSLog(@"date2 is %@", date2);
//格式化日期类
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy年MM月dd日 HH小时mm分钟ss秒 ZZZZ"];
//将日期按照格式化类型转换成字符串
NSString *str = [df stringFromDate:date];
NSLog(@"str is %@", str);
//将字符串格式化为日期
NSDate *date3 = [df dateFromString:str];
NSLog(@"date3 is %@", date3);