NSDate日常使用方法总结

(注:持续更新)
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);
    
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,929评论 25 709
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 13,912评论 6 13
  • 一个 pygame函数或方法,在传递一个文件路径的参数时将接受unicode或者一个字节 (8位)或者ASCII码...
    3767d46199be阅读 3,701评论 0 0
  • 翻出自己画的一些人物,希望大家给点意见
    COLOR_EDDY阅读 3,443评论 3 5
  • 在一个无味的午后 你说 来 抱着你睡 可能是无聊的说辞 隔着屏幕 我却慌措 因为 容易当真 因为 体寒最怕被温暖后的贪恋
    偏执狂via阅读 1,139评论 0 0

友情链接更多精彩内容