NSDate 与 NSString 之间的相互转换

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值