基础使用
NSDate *date1=[NSDate date];//获得当前日期
NSLog(@"%@",date1); //结果:2014-07-16 07:25:28 +0000
NSDate *date2=[NSDate dateWithTimeIntervalSinceNow:100];//在当前日期的基础上加上100秒,注意在ObjC中多数时间单位都是秒
NSLog(@"%@",date2); //结果:2014-07-16 07:27:08 +0000
NSDate *date3=[NSDate distantFuture];//随机获取一个将来的日期
NSLog(@"%@",date3); //结果:4001-01-01 00:00:00 +0000
NSTimeInterval time=[date2 timeIntervalSinceDate:date1];//日期之差,返回单位为秒
NSLog(@"%f",time); //结果:100.008833
NSDate *date5=[date1 earlierDate:date3];//返回比较早的日期
NSLog(@"%@",date5); //结果:2014-07-16 07:25:28 +0000
//日期格式化
NSDateFormatter *formater1=[[NSDateFormatter alloc]init];
formater1.dateFormat=@"yy-MM-dd HH:mm:ss";
NSString *datestr1=[formater1 stringFromDate:date1];
NSLog(@"%@",datestr1); //结果:14-07-16 15:25:28
//字符串转化为日期
NSDate *date6=[formater1 dateFromString:@"14-02-14 11:07:16"];
NSLog(@"%@",date6); //结果:2014-02-14 03:07:16 +0000
比较两个时间的大小
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm";
NSString *dateStr1 = @"2017-07-10 8:30";
NSString *dateStr2 = @"2017-07-10 8:21";
NSComparisonResult result = [[dateFormatter dateFromString:dateStr1] compare:[dateFormatter dateFromString:dateStr2]];
// NSOrderedAscending = -1L, 升序
// NSOrderedSame,
// NSOrderedDescending降序
NSLog(@"%zd",result);
计算时间(NSDateComponents)
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm";
NSString *dateStr1 = @"2017-06-18 8:30";
NSDateComponents *components = [cal components:( NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | kCFCalendarUnitWeekday) fromDate:[dateFormatter dateFromString:dateStr1]];
NSLog(@"%zd____%zd____%zd____%zd",components.year,components.month,components.day,components.weekday);
- 打印结果:
- 通过compoent可以得到对应的年月日,==关键是weekday==,是指当天为本星期的第几天,其中星期天为1,星期六为7.
2017-06-12 10:07:49.506 DateComponentDemo[1187:37235] 2017____6____18____1
计算两个时间的时间差:
1、计算两个时间的时间差:
NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comp = [calendar components:NSCalendarUnitDay fromDate:[NSDate date] toDate:[NSDate date] options:0];
NSLog(@"%zd",comp.day);
时间加减
//2、时间加减:
NSDate *tempDate = [self.currentCalendar dateByAddingUnit:NSCalendarUnitDay value:i toDate:lastDate options:0];
本地化时间
NSDate* now = [NSDate date];
NSLocale* cn = [NSLocale currentLocale];
str = [now descriptionWithLocale:cn];
NSLog(@"%@",str);//Friday, July 6, 2018 at 11:05:17 AM China Standard Time
获取当前时间
// 获取当前时间
- (NSString *)currentDateStr{
NSDate *currentDate = [NSDate date];//获取当前时间,日期
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//设定时间格式,这里可以设置成自己需要的格式
// [dateFormatter setDateFormat:@"YYYY/MM/dd hh:mm:ss SS"];
[dateFormatter setDateFormat:@"YYYY/MM/dd hh:mm:ss SS"];
NSString *dateString = [dateFormatter stringFromDate:currentDate];
return dateString;
}
时间戳转时间
// 时间戳转时间
- (NSString *)getDateStringWithDate:(NSString *)dataStr{
// dataStr时间戳
NSTimeInterval time=[dataStr doubleValue]+28800;//因为时差问题要加8小时 == 28800 sec
NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
// NSLog(@"date:%@",[detaildate description]);
//实例化一个NSDateFormatter对象
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//设定时间格式,这里可以设置成自己需要的格式
// [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setDateFormat:@"MM月dd日 HH:mm"];
NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
return currentDateStr;
}