NSDate 与 NSString 之间的相互转换

来源:互联网 发布:网络招聘哪个好 编辑:程序博客网 时间:2024/05/19 02:03

1. 创建时间类NSDate

NSDate *today = [[NSDate alloc] init];  tomorrow = [today dateByAddingTimeInterval: secondsPerDay];yesterday = [today dateByAddingTimeInterval: -secondsPerDay];  
返回以当前时间为基准,然后过了secs秒的时间+ (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;  返回以2001/01/01 GMT为基准,然后过了secs秒的时间+ (id)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secs;返回以1970/01/01 GMT为基准,然后过了secs秒的时间+ (id)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;返回很多年以后的未来的某一天。+ (id)distantFuture;返回很多年以前的某一天。+ (id)distantPast;

2. 日期之间比较可用以下方法

与otherDate比较,相同返回YES- (BOOL)isEqualToDate:(NSDate *)otherDate;与anotherDate比较,返回较早的那个日期- (NSDate *)earlierDate:(NSDate *)anotherDate;与anotherDate比较,返回较晚的那个日期- (NSDate *)laterDate:(NSDate *)anotherDate;该方法用于排序时调用: 1. (NSComparisonResult)compare:(NSDate *)other;. 当实例保存的日期值与anotherDate相同时返回NSOrderedSame. 当实例保存的日期值晚于anotherDate时返回NSOrderedDescending. 当实例保存的日期值早于anotherDate时返回NSOrderedAscending

3. 取回时间间隔可用以下方法

以refDate为基准时间,返回实例保存的时间与refDate的时间间隔- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)refDate;以当前时间(Now)为基准时间,返回实例保存的时间与当前时间(Now)的时间间隔- (NSTimeInterval)timeIntervalSinceNow;以1970/01/01 GMT为基准时间,返回实例保存的时间与1970/01/01 GMT的时间间隔- (NSTimeInterval)timeIntervalSince1970;以2001/01/01 GMT为基准时间,返回实例保存的时间与2001/01/01 GMT的时间间隔- (NSTimeInterval)timeIntervalSinceReferenceDate;以2001/01/01 GMT为基准时间,返回当前时间(Now)与2001/01/01 GMT的时间间隔+ (NSTimeInterval)timeIntervalSinceReferenceDate;

4. NSDateFormatter 的一些格式介绍

NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];[dateFormatter setDateFormat:@"yyyy年MM月dd日#EEEE"];EEEE为星期几,EEE为周几[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

5. NSDate 与 NSString 的互相转换:

NSDate 转换为 NSString:// 将当前时间以字符串形式输出NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];由 NSString 转换为 NSDate:NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];NSDate *date = [dateFormatter dateFromString:@"2010-08-04 16:01:03"];

6. 有的需求是要转换成类似于几分钟前,几天前的形式

**后台数据格式一:NSString *time = @"2016-07-01 09:52:00";======**// 距离1970年有多少秒    NSDateFormatter *formatter11 = [[NSDateFormatter alloc] init];    [formatter11 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];    NSDate *date11 = [formatter11 dateFromString:time];    NSTimeInterval dis = [date11 timeIntervalSince1970];// 时间换算    NSDate *currentDate = [NSDate date];  //(当前时间)    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:dis];    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];    formatter.dateFormat = @"yyyy-MM-dd";    NSString *string = [formatter stringFromDate:date];    NSTimeInterval spaceTime = [currentDate timeIntervalSinceDate:date];    int second = (int)spaceTime;    int minute = second / 60;    int hour = minute / 60;    if (minute < 1) {        _string = [NSString stringWithFormat:@"%@",@"刚刚"];    }else if (hour < 1 && minute >= 1) {        _string = [NSString stringWithFormat:@"%d分钟前",minute];    }else if (hour >= 1 && hour < 24) {        _string = [NSString stringWithFormat:@"%d小时前",hour];    }else if (hour >= 24 && hour < 10 * 24) {        _string = [NSString stringWithFormat:@"%d天前",hour / 24];    }else if (hour > 24 * 10) {        _string = [NSString stringWithFormat:@"%@",string];    }    NSLog(@"%@",_string);// 输出结果为:刚刚
**后台数据格式二:NSInteger secondData = 1000000000000;======**// 时间换算    NSDate *currentDate = [NSDate date];    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:secondData];    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];    formatter.dateFormat = @"yyyy-MM-dd";    NSString *string = [formatter stringFromDate:date];    NSTimeInterval spaceTime = [currentDate timeIntervalSinceDate:date];    int second = (int)spaceTime;    int minute = second / 60;    int hour = minute / 60;    if (minute < 1) {        self.timeLabel.text = @"刚刚";    }else if (hour < 1 && minute >= 1) {        self.timeLabel.text = [NSString stringWithFormat:@"%d分钟前",minute];    }else if (hour >= 1 && hour < 24) {        self.timeLabel.text = [NSString stringWithFormat:@"%d小时前",hour];    }else if (hour >= 24 && hour < 10 * 24) {        self.timeLabel.text = [NSString stringWithFormat:@"%d天前",hour / 24];    }else if (hour > 24 * 10) {        self.timeLabel.text = string;    }
0 0
原创粉丝点击