3)获取指定日期的月份有多少天
示例:
NSString *dateString = @"2015/10/08 3:2:34";
NSDate *theDate = [dateFormatter dateFromString:dateString];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSRange MonthRange = [gregorian rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:theDate];
NSInteger sumDays = MonthRange.length;
self.timeLabel2.text = [NSString stringWithFormat:@"%ld天", sumDays];
获取到了一个月份有多少天后,自然就可以想到下一个功能就是准确地从指定日期月份得到指定月份的下一个月份的第一天,这个功能常被用于日历的生成
示例:
NSString *dateString = @"2015/10/08 3:2:34";
NSDate *theDate = [dateFormatter dateFromString:dateString];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSRange MonthRange = [gregorian rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:theDate];
NSInteger sumDays = MonthRange.length;
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay fromDate:theDate];
NSInteger day = components.day;
NSDate *firstDate = [theDate dateByAddingTimeInterval:60 * 60 * 24 * (sumDays - day + 1)];
self.timeLabel2.text = [dateFormatter stringFromDate:firstDate];
4)时间间隔和日期的相互转换
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];
NSTimeInterval timeInter = [theDate timeIntervalSince1970] * 1000;
NSDate *sameDate = [NSDate dateWithTimeIntervalSince1970:timeInter/1000];
self.timeLabel2.text = [dateFormatter stringFromDate:sameDate];
5)判断某一天是不是昨天、今天、明天、周末等
代码如下:
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.view addSubview:label];
NSCalendar *calendar = [NSCalendar currentCalendar];
BOOL isToday = [calendar isDateInToday:theDate];
BOOL isYesterday = [calendar isDateInYesterday:theDate];
BOOL isTomorrow = [calendar isDateInTomorrow:theDate];
BOOL isInWeek = [calendar isDateInWeekend:theDate];
NSString *labelString = @"";
if (isInWeek) {
labelString = @"周末";
}
if (isYesterday) {
labelString = @"昨天";
}
if (isTomorrow) {
labelString = @"明天";
}
if (isToday) {
labelString = @"今天";
}
label.text = labelString;
6)NSDate时间显示样式
* 发表时间和当前时间对比,显示发表时间为:
效果:
1>今年
1)今天
1分钟内 , 刚刚,1小时内,xx分钟前
其他
xx小时前
2)昨天
昨天 18:56:34
其他
06-23 19:56:23
2>非今年
2014-05-08 18:45:30
给NSDate添加一个分类
@implementation NSDate (HLExtension)
/**
* 比较两个日期之间的差值
*/
- (NSDateComponents *)deltaFromDate:(NSDate *)fromDate
{
// 日历
NSCalendar *calender = [NSCalendar currentCalendar];
// 比较时间
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
// 返回日期的差值
return [calender components:unit fromDate:fromDate toDate:self options:0];
}
/**
* 判断是否为今年
*/
- (BOOL)isThisYear
{
// 日历
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger nowYear = [calendar component:NSCalendarUnitYear fromDate:[NSDate date]];
NSInteger selfYear = [calendar component:NSCalendarUnitYear fromDate:self];
return nowYear == selfYear;
}
/**
* 判断是否为今天
*/
- (BOOL)isToday
{
// 2015-07-28 18:30:00 -> 2015-07-28
// 日期格式化类
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";
// 日期转化为字符串
NSString *nowString = [fmt stringFromDate:[NSDate date]];
NSString *selfString = [fmt stringFromDate:self];
return [nowString isEqualToString:selfString];
}
/**
* 判断是否为昨天
*/
-(BOOL)isYesterDay
{
// 2015-07-28 18:30:00 -> 2015-07-28
// 日期格式化类
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";
// 转换日期
NSDate *nowDate = [fmt dateFromString:[fmt stringFromDate:[NSDate date]]];
NSDate *selfDate = [fmt dateFromString:[fmt stringFromDate:self]];
// 日历(比较时间)
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *cmps = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:selfDate toDate:nowDate options:0];
return cmps.year == 0 && cmps.month == 0 && cmps.day == 1;
}
@end
//在使用的时候进行相应的判断,就可以返回期望的时间格式
/**
* 重写create_time的get方法
*/
- (NSString *)create_time{
// 日期格式化类
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
// 帖子创建时间
NSDate *createDate = [fmt dateFromString:_create_time];
if (createDate.isThisYear) { // 今年
if (createDate.isToday) { // 今天
NSDateComponents *cmps = [[NSDate date] deltaFromDate:createDate];
if (cmps.hour >= 1) { // 1个小时 > 时间差
return [NSString stringWithFormat:@"%zd小时前", cmps.hour];
} else if (cmps.minute >= 1) { // 1个小时 > 时间差 > 1分钟
return [NSString stringWithFormat:@"%zd分钟前", cmps.minute];
}else { // 1分钟 > 时间差
return @"刚刚";
}
} else if (createDate.isYesterDay) {
fmt.dateFormat = @"昨天 HH:mm:ss";
return [fmt stringFromDate:createDate];
} else { // 其他
fmt.dateFormat = @"MM-dd HH:mm:ss";
return [fmt stringFromDate:createDate];
}
} else { // 非今年
return _create_time;
}
}
7)判断今天是周几
NSDate *date = [NSDate date];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: date];
NSDate *localeDate = [date dateByAddingTimeInterval: interval];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
comps = [calendar components:unitFlags fromDate:localeDate];
return [comps weekday]
参考:
NSDate时间显示样式