美文网首页
NSDate日常使用方法总结

NSDate日常使用方法总结

作者: 奋撸小菜鸟 | 来源:发表于2016-09-12 15:06 被阅读14次

    (注:持续更新)
    1、时间转时间戳:

    - (void)intervalTimeWithDate {
        //当前时间
        NSDate *date = [NSDate date];
        
        NSTimeInterval timeInterval = [date timeIntervalSince1970]*1000;
        
        NSLog(@"currentDate timeInterval = %f",timeInterval);
    }
    

    2、时间戳转时间:

    - (void)dateWithIntervalTime {
        //当前时间
        NSTimeInterval timeInterval = 1474283450000;
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval/1000];
        
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        NSString *dateStr = [formatter stringFromDate:date];
        NSLog(@"date = %@",dateStr);
    }
    

    3、倒计时

    - (void)timeIntervalCompare {
        
        NSTimeInterval timerInterval1 = 1474283450000;
        NSTimeInterval timerInterval2 = 1473483450000;
        
        NSInteger timeInterval = (timerInterval2 - timerInterval1)/1000;
        
        int day = (int)(timeInterval/86400);
        int hour = (int)((timeInterval - day*86400)/3600);
        int minute = (int)(timeInterval - day*86400 - hour*3600)/60;
        int second = (int)(timeInterval - day*86400 - hour*3600 - minute*60);
        
        NSLog(@"%d天%d时%d分%d秒",day,hour,minute,second);
        
    }
    

    相关文章

      网友评论

          本文标题:NSDate日常使用方法总结

          本文链接:https://www.haomeiwen.com/subject/ylypsttx.html