zoukankan      html  css  js  c++  java
  • 时间与时间戳的转换

    时间戳是一种时间表示,定义从格林尼治时间1970年01月01日00时00分00秒起至现在的总秒数
    //现在时间 NSDate *nowTime = [NSDate date]; //获取时区 NSTimeZone *zone = [NSTimeZone systemTimeZone]; NSInteger interVal = [zone secondsFromGMTForDate:nowTime]; NSDate *localTime = [nowTime dateByAddingTimeInterval:interVal];//本地时间 //时间戳转为时间 NSString *time = @"1400386922"; CGFloat dTime = [time floatValue];
        NSDate *publishTime = [NSDate dateWithTimeIntervalSince1970:dTime];
        NSLog(@"%@", publishTime);
        
        //计算时间间隔(localTime - publishTime)
        NSTimeInterval timeInterval = [localTime timeIntervalSinceDate:publishTime];
        NSLog(@"%f", timeInterval);
        if (timeInterval < 60) {
           
            NSString *time = @"刚刚";
             NSLog(@"刚刚!!");
        }
        if (timeInterval >= 60 && timeInterval < 3600) {
            int a = timeInterval / 60;
            NSString *time = [NSString stringWithFormat:@"%d分钟前", a];
            NSLog(@"%@", time);
        }
        if (timeInterval >= 3600 && timeInterval < 3600 * 24) {
            int a = timeInterval / 3600;
           
            NSString *time = [NSString stringWithFormat:@"%d小时前", a];
            NSLog(@"%@", time);
        }
        if (timeInterval >= 3600 * 24 && timeInterval < 3600 * 24 * 31) {
            int a = timeInterval / (3600 * 24);
            NSString *time = [NSString stringWithFormat:@"%d天前", a];
           NSLog(@"%@", time);
        }
    

      

  • 相关阅读:
    python之np.tile()
    python中easydict的简单使用
    Python字典(Dictionary)update()方法
    Flutter学习之导航与数据的传输
    Flutter学习之重叠布局
    Flutter学习之重叠布局
    Flutter学习之纵向布局
    Flutter学习之纵向布局
    Flutter学习之GridView
    Flutter学习之GridView
  • 原文地址:https://www.cnblogs.com/NatureZhang/p/3748467.html
Copyright © 2011-2022 走看看