首页 > 代码库 > 计算时间
计算时间
NSDate+MJ.h
#import <Foundation/Foundation.h>@interface NSDate (MJ)/** * 是否为今天 */- (BOOL)isToday;/** * 是否为昨天 */- (BOOL)isYesterday;/** * 是否为今年 */- (BOOL)isThisYear;/** * 返回一个只有年月日的时间 */- (NSDate *)dateWithYMD;/** * 获得与当前时间的差距 */- (NSDateComponents *)deltaWithNow;@end
NSDate+MJ.m
//// NSDate+MJ.m// ItcastWeibo//// Created by apple on 14-5-9.// Copyright (c) 2014年 itcast. All rights reserved.//#import "NSDate+MJ.h"@implementation NSDate (MJ)/** * 是否为今天 */- (BOOL)isToday{ NSCalendar *calendar = [NSCalendar currentCalendar]; int unit = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear; // 1.获得当前时间的年月日 NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]]; // 2.获得self的年月日 NSDateComponents *selfCmps = [calendar components:unit fromDate:self]; return (selfCmps.year == nowCmps.year) && (selfCmps.month == nowCmps.month) && (selfCmps.day == nowCmps.day);}/** * 是否为昨天 */- (BOOL)isYesterday{ // 2014-05-01 NSDate *nowDate = [[NSDate date] dateWithYMD]; // 2014-04-30 NSDate *selfDate = [self dateWithYMD]; // 获得nowDate和selfDate的差距 NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *cmps = [calendar components:NSCalendarUnitDay fromDate:selfDate toDate:nowDate options:0]; return cmps.day == 1;}- (NSDate *)dateWithYMD{ NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = @"yyyy-MM-dd"; NSString *selfStr = [fmt stringFromDate:self]; return [fmt dateFromString:selfStr];}/** * 是否为今年 */- (BOOL)isThisYear{ NSCalendar *calendar = [NSCalendar currentCalendar]; int unit = NSCalendarUnitYear; // 1.获得当前时间的年月日 NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]]; // 2.获得self的年月日 NSDateComponents *selfCmps = [calendar components:unit fromDate:self]; return nowCmps.year == selfCmps.year;}- (NSDateComponents *)deltaWithNow{ NSCalendar *calendar = [NSCalendar currentCalendar]; int unit = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; return [calendar components:unit fromDate:self toDate:[NSDate date] options:0];}@end
// 创建格式化类 NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; // 设置格式 _created_at = @"Mon Jul 14 16:13:07 +0800 2014"; fmt.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy"; #warning 真机调试(真机显示时间,要写上这句话) fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; // 获得发微博的具体时间 NSDate *createDate = [fmt dateFromString:_created_at]; // 判断是否为今年 if (!createDate.isThisYear) { fmt.dateFormat = @"yyyy-MM-dd"; return [fmt stringFromDate:createDate]; } if (!createDate.isToday && !createDate.isYesterday) { // 不是今天,也不是昨天 fmt.dateFormat = @"MM-dd HH:mm"; return [fmt stringFromDate:createDate]; } if (!createDate.isToday) { fmt.dateFormat = @"昨天 HH:mm"; return [fmt stringFromDate:createDate]; } NSDateComponents *cmps = [createDate deltaWithNow]; if (!cmps.hour >= 1 && !cmps.minute >= 1) { return @"刚刚"; } if (!cmps.hour >= 1) { return [NSString stringWithFormat:@"%d分钟前", cmps.minute]; } return [NSString stringWithFormat:@"%d小时前", cmps.hour];
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。