zoukankan      html  css  js  c++  java
  • 计算两个时间戳之间相差的时间

    1。//获取当前时间戳

    - (NSInteger )getCurrentTimeStamp {

        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

        [formatter setDateStyle:NSDateFormatterMediumStyle];

        [formatter setTimeStyle:NSDateFormatterShortStyle];

        [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制

        //设置时区,这个对于时间的处理有时很重要

        NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];

        [formatter setTimeZone:timeZone];

        NSDate *datenow = [NSDate date];//现在时间

        

        //时间转时间戳的方法:

        NSInteger timeSp = [[NSNumber numberWithDouble:[datenow timeIntervalSince1970]] integerValue];

        return timeSp;

    }

    2。//获取两个时间戳的相差时间

     -(Nsstring)getDateTimeWithLastTime:(Nsinteger )lastTime  NowTime:(Nsinteger)nowTime {

        NSTimeInterval value = nowTime   - lastTime;

        int second = (int)value %60;//秒

        int minute = (int)value /60%60;

        int house = (int)value / (24 *3600)%3600;//时

        int day = (int)value / (24 *3600);

        NSString *str;

        if (day != 0) {

            str = [NSString stringWithFormat:@"耗时%d天%d小时%d分%d秒",day,house,minute,second];

        }else if (day==0 && house !=0) {

            str = [NSString stringWithFormat:@"耗时%d小时%d分%d秒",house,minute,second];

        }else if (day==0 && house==0 && minute!=0) {

            str = [NSString stringWithFormat:@"耗时%d分%d秒",minute,second];

        }else{

            str = [NSString stringWithFormat:@"耗时%d秒",second];

        }

        NSLog(@" %@ ",str);

      return  str;

    }

  • 相关阅读:
    装饰器模式(学习笔记10)
    软件设计七大原则 (学习笔记2)
    设计模式(学习笔记1)
    桥接模式 (学习笔记9)
    Mini2440裸机开发之LCD基础
    配置中心java客户端实现
    高效CSS写法
    整理电脑里面的前端开发中的小Tips【及时更新】
    IE6 position:fixed (固定定位)的解决方案
    jquery 文档操作
  • 原文地址:https://www.cnblogs.com/Lovexiaohuzi/p/7797342.html
Copyright © 2011-2022 走看看