1,读取当前的日期格式,然后自动转化为yyyy-MM-dd HH:mm:ss 0000
//日期格式器 NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; // E:星期几 // M:月份 // d:几号(这个月的第几天) // H:24小时制的小时 // m:分钟 // s:秒 // y:年 // _created_at == Thu Oct 16 17:06:25 +0800 2014 //读取当前的日期格式,然后自动转化为yyyy-MM-dd HH:mm:ss 0000 fmt.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";
2,如果是真机调试,转换这种欧美时间,需要设置locale
fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
3,字符串互转日期
//字符串转为日期 NSDate *creatDate = [fmt dateFromString:_created_at]; //日期转为字符串 NSString *str = [fmt stringFromdata:_created_at];
4,对比两个日期的差值
//日历对象 NSCalendar *calendar = [NSCalendar currentCalendar]; //计算两个日期之间的差值,NSCalendarUnit枚举代表想获得哪些差值 NSCalendarUnit unit = kCFCalendarUnitYear | kCFCalendarUnitMonth | kCFCalendarUnitDay | kCFCalendarUnitHour | kCFCalendarUnitMinute; //获得对比对象 NSDateComponents *cmps = [calendar components:unit fromDate:creatDate toDate:now options:0];
5,判断是否为今天,昨天,今年
- (BOOL)isThisYear:(NSDate *)creatDate{#warning //取出当前的日期--年月日 NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *dateCmps = [calendar components:NSCalendarUnitYear fromDate:creatDate]; NSDateComponents *nowCmps = [calendar components:NSCalendarUnitYear fromDate:[NSDate date]]; return dateCmps.year == nowCmps.year;}- (BOOL)isToday:(NSDate *)creatDate{ /** 另一种方法*/ NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = @"yyyy-MM-dd"; NSDate *now = [NSDate date]; NSString *nowStr = [fmt stringFromDate:now]; NSString *dateStr = [fmt stringFromDate:creatDate]; return [nowStr isEqualToString:dateStr];}- (BOOL)isYesterday:(NSDate *)creatDate{ NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = @"dd"; NSDate *now = [NSDate date]; NSString *nowStr = [fmt stringFromDate:now]; NSString *dateStr = [fmt stringFromDate:creatDate]; BOOL isSure = NO; if (nowStr.intValue - dateStr.intValue == 1) { isSure = YES; } //WBLog(@"%d",xx); return isSure;}
6,微博时间显示
//1,美国时间转为中国时间//2, 转为各种模式显示-(NSString *)created_at{ _created_at = @"Thu Aug 26 10:35:34 +0800 2015"; //日期格式器 NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; // E:星期几 // M:月份 // d:几号(这个月的第几天) // H:24小时制的小时 // m:分钟 // s:秒 // y:年 // _created_at == Thu Oct 16 17:06:25 +0800 2014 //读取当前的日期格式,然后自动转化为yyyy-MM-dd HH:mm:ss 0000 fmt.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";#warning 如果是真机调试,转换这种欧美时间,需要设置locale fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; //微博的创建日期 NSDate *creatDate = [fmt dateFromString:_created_at]; //当前时间 NSDate *now = [NSDate date]; //日历对象(方便两个日期之前的差距比较) NSCalendar *calendar = [NSCalendar currentCalendar]; //计算两个日期之间的差值,NSCalendarUnit枚举代表想获得哪些差值 NSCalendarUnit unit = kCFCalendarUnitYear | kCFCalendarUnitMonth | kCFCalendarUnitDay | kCFCalendarUnitHour | kCFCalendarUnitMinute; NSDateComponents *cmps = [calendar components:unit fromDate:creatDate toDate:now options:0]; if ([self isThisYear:creatDate]) { if ([self isYesterday:creatDate]) { fmt.dateFormat = @"昨天 HH:mm"; return [fmt stringFromDate:creatDate]; }else if ([self isToday:creatDate]){ if (cmps.hour > 1) { return [NSString stringWithFormat:@"%d小时前",cmps.hour]; }else if (cmps.minute > 1){ return [NSString stringWithFormat:@"%d分钟前",cmps.minute]; }else{ return @"刚刚"; } }else{//今年其他日子 fmt.dateFormat = @"MM-dd HH:mm"; return [fmt stringFromDate:creatDate]; } }else{//非今年 fmt.dateFormat = @"yyyy-MM-dd HH:mm"; return [fmt stringFromDate:creatDate]; } return _created_at;}- (BOOL)isThisYear:(NSDate *)creatDate{#warning //取出当前的日期--年月日 NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *dateCmps = [calendar components:NSCalendarUnitYear fromDate:creatDate]; NSDateComponents *nowCmps = [calendar components:NSCalendarUnitYear fromDate:[NSDate date]]; return dateCmps.year == nowCmps.year;}- (BOOL)isToday:(NSDate *)creatDate{ /** 另一种方法*/ NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = @"yyyy-MM-dd"; NSDate *now = [NSDate date]; NSString *nowStr = [fmt stringFromDate:now]; NSString *dateStr = [fmt stringFromDate:creatDate]; return [nowStr isEqualToString:dateStr];}- (BOOL)isYesterday:(NSDate *)creatDate{ NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = @"dd"; NSDate *now = [NSDate date]; NSString *nowStr = [fmt stringFromDate:now]; NSString *dateStr = [fmt stringFromDate:creatDate]; BOOL isSure = NO; if (nowStr.intValue - dateStr.intValue == 1) { isSure = YES; } //WBLog(@"%d",xx); return isSure;}
5.时间戳
//时间字符串 -> 时间戳 NSString *string = @"1712312312312"; NSTimeInterval second = string.longLongValue / 1000.0; //时间戳 -> NSDate NSDate *date = [NSDate dateWithTimeIntervalSince1970:second];