iOS 时间戳 时间串 比较和转换

1.时间转字符串

NSDate * date = [NSDate date];
    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm"];
    NSString * dateStr = [formatter stringFromDate:date];
    NSLog(@"%@",dateStr);

2016-06-29 02:30


2.字符串转时间

NSString * dateStr = @"2016-06-30 12:00:00 0";
    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];
    NSDate * date = [formatter dateFromString:dateStr];
    NSLog(@"%@",date);

3.时间戳转时间串

    CGFloat time = [startTime doubleValue]/1000;
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy.MM.dd"];
    NSString * str1 = [dateFormatter stringFromDate: detaildate];


4.时间戳与当前时间对比,做倒计时
 CGFloat time1 = [_order.gmt_expire doubleValue]/1000;
        NSDate *date1=[NSDate dateWithTimeIntervalSince1970:time1];
        
        NSTimeInterval time2 =[date1 timeIntervalSinceNow];
        int min=((int)time2)/60;
        cell.warnLabel.text =[NSString stringWithFormat:@"请您于%ld分钟内完成支付,否则订单将被取消",(long)min];


5.时间转时间戳

 NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a=[date timeIntervalSince1970]*1000; // *1000 是精确到毫秒,不乘就是精确到秒
    NSString *timeString = [NSString stringWithFormat:@"%.0f", a]; //转为字符型
    NSLog(@"%@",timeString);






  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值