zoukankan      html  css  js  c++  java
  • 时间处理NSDate

    1.

    NSTimeInterval 时间间隔double
        NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];  //2001/1/1时间以秒数的形式表示
        NSLog(@"double=%f",now);//444907472.111701
        //
        NSDate *datec = [NSDate dateWithTimeIntervalSinceNow:100];
        NSTimeInterval seconds = [datec timeIntervalSinceNow]; //日期于当前日期的相差时间
        NSLog(@"%f",seconds);//以秒数的形式
        //
        NSDate *datea = [NSDate dateWithTimeIntervalSince1970:0];
        NSDate *dateb = [NSDate dateWithTimeInterval:50 sinceDate:datea];
        NSTimeInterval secondsa = [datea timeIntervalSinceDate:dateb];//日期与日期的相差时间
        NSLog(@"%f",secondsa);
        /////////////////////////////////////////////////////NSDate的创建/////////////////////////////////////////////////////////////////////////////
        NSDate *date = [NSDate date];
        NSLog(@"date=%@",date);
        //以当前时间为基准
        NSDate *timeInterval = [[NSDate alloc]initWithTimeIntervalSinceNow:20];
        NSLog(@"timeInterval= %@",timeInterval);
        //以1970/1/1为基准
        NSDate *since1970= [[NSDate alloc]initWithTimeIntervalSince1970:-20];
        NSLog(@"since1970=%@",since1970);
        //以2001/1/1为基准
        NSDate *sinceReferenceDate = [[NSDate alloc]initWithTimeIntervalSinceReferenceDate:80];
        NSLog(@"sinceReferenceDate=%@",sinceReferenceDate);
        //以某日期为基准
        NSDate *date2 = [[NSDate alloc]initWithTimeInterval:10 sinceDate:date];
        NSLog(@"date2=%@",date2);
        //
        NSLog(@"future date = %@",[NSDate distantFuture]);
        NSLog(@"past date = %@",[NSDate distantPast]);

    2015-02-06 17:49:38.290 MeiTeamDemo[13580:203531] double=444908978.290587
    2015-02-06 17:49:38.291 MeiTeamDemo[13580:203531] 99.999974
    2015-02-06 17:49:38.291 MeiTeamDemo[13580:203531] -50.000000
    2015-02-06 17:49:38.292 MeiTeamDemo[13580:203531] date=2015-02-06 09:49:38 +0000
    2015-02-06 17:49:38.293 MeiTeamDemo[13580:203531] timeInterval= 2015-02-06 09:49:58 +0000
    2015-02-06 17:49:38.293 MeiTeamDemo[13580:203531] since1970=1969-12-31 23:59:40 +0000
    2015-02-06 17:49:38.293 MeiTeamDemo[13580:203531] sinceReferenceDate=2001-01-01 00:01:20 +0000
    2015-02-06 17:49:38.293 MeiTeamDemo[13580:203531] date2=2015-02-06 09:49:48 +0000
    2015-02-06 17:49:38.293 MeiTeamDemo[13580:203531] future date = 4001-01-01 00:00:00 +0000
    2015-02-06 17:49:38.293 MeiTeamDemo[13580:203531] past date = 0000-12-30 00:00:00 +0000

    2.统计时间耗时利器

    CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();  
    // do something  
     CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();  
     NSLog(@"time cost: %0.3f", end - start);  

     3.

    NSTimeInterval interval = [_startDate timeIntervalSinceNow];//日期相差的秒数
        
        NSString *title = [NSString stringWithFormat:@"做题进度:%ld/%ld 道  剩余时间:%d:%d",_indexOfQuestion + 1, self.dataSource.count ,(int)interval/60,(int)interval%60 ];
  • 相关阅读:
    如何面试你的面试官
    给老婆的一篇文章
    机器学习:选对时机直线超车
    面试技巧:带走面试官的节奏
    微信红包的随机算法是怎样实现的?
    全栈技术导图
    AI时代:推荐引擎正在塑造人类
    冲顶大会APP技术选型及架构设计
    多域名解析及延伸知识点
    在编程中为所欲为[圣诞版]
  • 原文地址:https://www.cnblogs.com/huen/p/4277762.html
Copyright © 2011-2022 走看看