标签:
// 获取当前的日期
NSDate *date1 = [NSDate date];
NSLog(@"data1 = %@", date1);
// 获取三天前的日期
NSDate *date2 = [[NSDate alloc]initWithTimeIntervalSinceNow:-3*3600*24];
NSLog(@"date2 = %@", date2);
// 获取一天后的日期时间
NSDate *date3 = [[NSDate alloc]initWithTimeIntervalSinceNow:3600*24];
NSLog(@"date3 = %@", date3);
// 比较两个日期时间的早晚
NSDate *earlierDate = [date1 earlierDate:date2];
NSLog(@"earlierDate = %@", earlierDate);
// 比较两个时间相差的秒数
NSTimeInterval timeInterval1 = [date1 timeIntervalSinceNow];
NSLog(@"timeInterval1 = %f", timeInterval1);
NSTimeInterval timeInterval2 = [date1 timeIntervalSinceDate:date2];
NSLog(@"timeInterval1 = %f", timeInterval2);
// 时间格式化
// 1.日期时间转自定义格式化字符串
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc]init];
[dateFormatter1 setDateFormat:@"公元yyyy年MM月dd日 HH:mm:ss"];
NSString *resultStr = [dateFormatter1 stringFromDate:date1];
NSLog(@"date1 = %@", resultStr);
//2.日期时间字符串转NSDate
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc]init];
[dateFormatter2 setDateFormat:@"公元yyyy年MM月dd日 HH:mm:ss"];
NSDate *date4 = [dateFormatter2 dateFromString:resultStr];
NSLog(@"dade4 = %@", date4);
标签:
原文地址:http://www.cnblogs.com/xiangjune/p/5649323.html