一.时间戳
二.Date的基础介绍
三.NSCalendar的基础介绍
一.时间戳
- 时间戳:从1970年1月1号 00:00:00开始走过的毫秒数。IOS端默认生成的时间戳是10位的,如果服务器返回的13位需要除以1000例如:
//字符串 -> 时间戳
NSString * timeStampString = @"1423189125435"
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timeStampString doubleValue] / 1000];
- 时间戳转Date
NSDate *date = [NSDate dateWithTimeIntervalSince1970:second];
二.Date的基础介绍
1.时间和时区 (北京为东8区)
- 创建date对象即时间对象
NSDate *dats = [[NSDate alloc]init];
- 获取当前时间(本初子午线的时间)
NSDate *date = [NSDate date];
- 获取当前所在地区的时区
NSTimeZone *zone = [NSTimeZone systemTimeZone];
- 获取当前时区和指定时间的时差
NSTimeInterval seconds = [zone secondsFromGMTForDate:date];
NSDate *nowDate = [date dateByAddingTimeInterval:secend];
2.字符串于Date的相互转化
- 字符串-> Date
NSString *createdString = @"2018-11-20";
//时间格式化对象
NSDateFormatter *dateformate = [[NSDateFormatter alloc]init];
dateformate.dateFormat = @"yyyy-MM-dd";
//会自动减去8个小时
NSDate *creatDate = [dateformate dateFromString:createdString];
NSLog(@"%@",creatDate);
- Date ->字符串
NSDate *nowdate = [NSDate date]; NSString *nowstring = [dateformate stringFromDate:nowdate];
总结:Date 和字符串相互转换需要一个桥梁NSDateFormatter,NSDateFormatter 的格式有很多比如:yyyy/MM/dd HH/mm/ss 、yyyy年MM月dd日 HH时mm分ss秒、yyyy/MM/dd等。
3.通过Date比较两时间 (NSComparisonResult)
NSString *createdString = @"2018-11-20";
NSDateFormatter *dateformate = [[NSDateFormatter alloc]init];
dateformate.dateFormat = @"yyyy-MM-dd";//自动减去8个小时
NSDate *creatDate = [dateformate dateFromString:createdString];
NSLog(@"%@",creatDate);
NSDate *nowdate = [NSDate date];
NSComparisonResult result = [creatDate compare:nowdate];
if (result == NSOrderedAscending) {
NSLog(@"nowdate>creatDate");
}
else if(result == NSOrderedDescending)
{
NSLog(@"creatDate>nowdate");
}
else if(result == NSOrderedSame)
{
NSLog(@"creatDate=nowdate");
}
4.时间的加减 (通过时间戳)
NSDate*nowDate = [NSDate date];
NSTimeInterval interval =24*60*60*1; //1:天数
NSDate*date1 = [nowDate initWithTimeIntervalSinceNow:+interval];//加一天
NSDate*date1 = [nowDate initWithTimeIntervalSinceNow:-interval];//减一天
三.NSCalendar的基础介绍
NSDate * date = [NSDate date];
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; // 指定日历的算法 NSCalendarIdentifierGregorian,NSGregorianCalendar
// NSDateComponent 可以获得日期的详细信息,即日期的组成
NSDateComponents *comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitWeekOfMonth|NSCalendarUnitWeekday fromDate:date];
NSLog(@"年 = year = %ld",comps.year);
NSLog(@"月 = month = %ld",comps.month);
NSLog(@"日 = day = %ld",comps.day);
NSLog(@"时 = hour = %ld",comps.hour);
NSLog(@"分 = minute = %ld",comps.minute);
NSLog(@"秒 = second = %ld",comps.second);
NSLog(@"星期 =weekDay = %ld ",comps.weekday);// 从周日开始算的
-
通过NSCalendar日期比较
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; // 指定日历的算法
NSDate * currentDate = [NSDate date]; // 这个日期可以你自己给定
NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay
inUnit: NSCalendarUnitMonth
forDate:currentDate];
NSLog(@"%ld",range.length);//日期比较 NSDateComponents *cmps = [calendar components:NSCalendarUnitYear fromDate:[NSDate dateWithTimeIntervalSince1970:0] toDate:[NSDate date] options:0]; NSLog(@"%li",(long)cmps.year);
NSCalendarUnitYear这个参数可选:
NSCalendarUnitYear
NSCalendarUnitMonth
NSCalendarUnitDay
NSCalendarUnitHour
NSCalendarUnitMinute
NSCalendarUnitSecond
NSCalendarUnitWeekday
最后写一个小的方法,项目中经常用到字符串类型的时间大小对比
//对比两个时间大小。
-(BOOL)comparedate:(NSString *)firstTime secendtime:(NSString *)secendTime andNSDateFormatter:(NSDateFormatter *)formatter
{
NSDateFormatter *nowformatter;
if (formatter) {
nowformatter = formatter;
}
else
{
nowformatter = [[NSDateFormatter alloc]init];
nowformatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
}
NSDate *firestDate =[nowformatter dateFromString:firstTime];
NSDate *secendDate = [nowformatter dateFromString:secendTime];
NSComparisonResult result = [firestDate compare:secendDate];
if (result == NSOrderedAscending) {
return NO;
}
else if(result == NSOrderedDescending)
{
return YES;
}
else
{
return YES;
}
}
以上都是一些基础知识,方便以后查阅。