1//当前时间创建NSDate
NSDate*myDate = [NSDatedate];
NSLog(@"myDate= %@",myDate);
2//从现在开始的24小时
NSTimeIntervalsecondsPerDay =24*60*60;
NSDate*tomorrow = [NSDatedateWithTimeIntervalSinceNow:secondsPerDay];
NSLog(@"myDate= %@",tomorrow);
3//根据已有日期创建日期
NSTimeIntervalsecondsPerDay1 =24*60*60;
NSDate*now = [NSDatedate];
NSDate*yesterDay = [nowaddTimeInterval:-secondsPerDay1];
NSLog(@"yesterDay= %@",yesterDay);
4//比较日期
BOOLsameDate = [nowisEqualToDate:yesterDay];
NSLog(@"sameDate= %lu",sameDate);
4.1//获取较早的日期
NSDate*earlierDate = [yesterDayearlierDate:now];
NSLog(@"earlierDate= %@",earlierDate);
4.2//较晚的日期
NSDate*laterDate = [yesterDaylaterDate:now];
NSLog(@"laterDate= %@",laterDate);
//两个日期之间相隔多少秒
NSTimeIntervalsecondsBetweenDates= [yesterDaytimeIntervalSinceDate:now];
NSLog(@"secondsBetweenDates=%lf",secondsBetweenDates);
//通过NSCALENDAR类来创建日期
NSDateComponents *comp = [[NSDateComponentsalloc]init];
[compsetMonth:06];
[compsetDay:01];
[compsetYear:2001];
NSCalendar *myCal = [[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *myDate1 = [myCaldateFromComponents:comp];
NSLog(@"myDate1 = %@",myDate1);
//从已有日期获取日期
unsignedunits = NSMonthCalendarUnit|NSDayCalendarUnit|NSYearCalendarUnit;
NSDateComponents*comp1 = [myCalcomponents:unitsfromDate:now];
NSIntegermonth = [comp1month];
NSIntegeryear = [comp1year];
NSIntegerday = [comp1day];
//NSDateFormatter实现日期的输出
NSDateFormatter*formatter
= [[NSDateFormatteralloc]init];
[formattersetDateStyle:NSDateFormatterFullStyle];//直接输出的话是机器码
//或者是手动设置样式[formatter setDateFormat:@"yyyy-mm-dd"];
NSString*string = [formatterstringFromDate:now];
NSLog(@"string= %@",string);
NSLog(@"formater= %@",formatter);
//获取日期格式对象
- (NSDateFormatter*)dateFormatter {
if(dateFormatter==nil) {
dateFormatter= [[NSDateFormatteralloc]init];
[dateFormattersetDateStyle:NSDateFormatterMediumStyle];
[dateFormattersetTimeStyle:NSDateFormatterNoStyle];
}
returndateFormatter;
}
将当前对时间显示出来
NSDate -> NSSTring
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy/MM/dd hh:mm:ss"];
self.timeLabel.text= [dateFormatter stringFromDate:date];
NSSTring -> NSDate
NSString *dateString = self.timeLabel.text;
NSDate *nowDate = [dateFormatter dateFromString:dateString];
self.timeLabel2.text= [dateFormatter stringFromDate:nowDate];
分别获取指定日期的年、月、日、时、分、秒
[dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
NSString*dateString =@"2016/10/12 3:2:34";
NSDate*theDate = [dateFormatter dateFromString:dateString];
NSDateComponents*com = [[NSCalendarcurrentCalendar] components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecondfromDate:theDate];NSIntegeryear = com.year;
NSIntegermonth = com.month;
NSIntegerday = com.day;
NSIntegerhour = com.hour;
NSIntegerminute = com.minute;NSIntegersecond = com.second;
self.timeLabel2.text = [NSStringstringWithFormat:@"%ld年%ld月%ld日 %02ld时:%02ld分:%02ld秒, year, month, day, hour, minute, second];
//上面的代码写法比较快,但是不够清楚,下面换种写法,感觉你就是大牛有木有
NSString*dateString =@"2015/09/06 3:2:34";
NSDate*theDate = [dateFormatter dateFromString:dateString];
NSCalendar*calendar = [NSCalendarcurrentCalendar];
NSUIntegerunitFlags = NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;NSDateComponents*cmp1 = [calendar components:unitFlags fromDate:theDate];
NSDateComponents*cmp2 = [calendar components:unitFlags fromDate:[NSDatedate]];
// 2.格式化日期
NSDateFormatter*formatter = [[NSDateFormatteralloc] init];
if([cmp1 day] == [cmp2 day]) {
// 今天
formatter.dateFormat =@"今天 HH:mm";
}elseif([cmp1 year] == [cmp2 year]) {
// 今年
formatter.dateFormat =@"MM-dd HH:mm";
}else{
formatter.dateFormat =@"yyyy-MM-dd HH:mm";
}
NSString*time = [formatter stringFromDate:theDate];
// 3.显示时间
self.timeLabel2.text = time;
//注意,通过改变NSDateFormatter的dateFormat表现形式可以实现各种你想要的时间表示形式,比如
NSString *dateString = @"2015/09/08 3:2:34";
NSDate *theDate = [dateFormatter dateFromString:dateString];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"'日期:'yyyy有鬼MM😊dd'时间:'HH好玩吗mm🐎ss";
self.timeLabel2.text= [formatter stringFromDate:theDate];
另外提到的一点是:要获取那一天是星期几的话把CalendarUnitWeekday加入就好了。
获取指定日期的月份有多少天
NSString*dateString = @"2015/10/08 3:2:34";
NSDate*theDate = [dateFormatter dateFromString:dateString];
NSCalendar*gregorian = [[NSCalendaralloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];NSRangeMonthRange = [gregorian rangeOfUnit:NSCalendarUnitDayinUnit:NSCalendarUnitMonthforDate:theDate];NSIntegersumDays = MonthRange.length;self.timeLabel2.text = [NSStringstringWithFormat:@"%ld天", sumDays];
获取到了一个月份有多少天后,自然就可以想到下一个功能就是准确地从指定日期月份得到指定月份的下一个月份的第一天,这个功能常被用于日历的生成
NSString*dateString =@"2015/10/08 3:2:34";NSDate*theDate = [dateFormatter dateFromString:dateString];NSCalendar*gregorian = [[NSCalendaralloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];NSRangeMonthRange = [gregorian rangeOfUnit:NSCalendarUnitDayinUnit:NSCalendarUnitMonthforDate:theDate];NSIntegersumDays = MonthRange.length;NSDateComponents*components = [[NSCalendarcurrentCalendar] components:NSCalendarUnitDayfromDate:theDate];NSIntegerday = components.day;NSDate*firstDate = [theDate dateByAddingTimeInterval:60*60*24* (sumDays - day +1)];self.timeLabel2.text = [dateFormatter stringFromDate:firstDate];
NSDate -> NSTimeInterval
NSString *dateString = @"2015/10/08 3:2:34";NSDate *theDate = [dateFormatter dateFromString:dateString];NSTimeInterval timeInter = [theDate timeIntervalSince1970] *1000;self.timeLabel2.text= [NSString stringWithFormat:@"1970年到%@的毫秒数为:%.2f", dateString, timeInter];
NSTimeInterval -> NSDate
NSString*dateString =@"2015/10/08 3:2:34";NSDate*theDate = [dateFormatter dateFromString:dateString];NSTimeIntervaltimeInter = [theDate timeIntervalSince1970] *1000;NSDate*sameDate = [NSDatedateWithTimeIntervalSince1970:timeInter/1000];self.timeLabel2.text = [dateFormatter stringFromDate:sameDate];
格式化输出 年月日 上下午 时分秒 星期几
显示效果如下:
代码如下:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy/MM/dd HH🐷mm🐎ss";
NSString *dateString = @"2015/09/08 3:2:34";
NSDate *theDate = [formatter dateFromString:dateString];
formatter.dateFormat = @"MM月dd日 a HH:mm EEEE";
UILabel *label= [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 300, 50)];
label.text = [formatter stringFromDate:theDate];[self.view addSubview:label];
判断某一天是不是昨天、今天、明天、周末等
代码如下:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy/MM/dd HH:mm:ss";
NSString *dateString = @"2015/09/20 3:2:34";
NSDate *theDate = [formatter dateFromString:dateString];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20,100,300,50)];
label.text= [formatter stringFromDate:theDate];
[self.viewaddSubview:label];
NSCalendar *calendar= [NSCalendar currentCalendar];
BOOL isToday = [calendarisDateInToday:theDate];
BOOL isYesterday = [calendarisDateInYesterday:theDate];
BOOL isTomorrow = [calendarisDateInTomorrow:theDate];
BOOL isInWeek = [calendarisDateInWeekend:theDate];
NSString *labelString = @"";
if (isInWeek) { labelString = @"周末";} if (isYesterday) { labelString = @"昨天";} if (isTomorrow) { labelString = @"明天";} if (isToday) { labelString = @"今天";}
label.text= labelString;