程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> ios-iOS 計算起始時間和終止時間的差

ios-iOS 計算起始時間和終止時間的差

編輯:編程綜合問答
iOS 計算起始時間和終止時間的差

簡單的就是說 a = 終止 - 起始
求大神 幫忙解答,
求代碼演示;

我是這樣算的

  • (void)viewDidAppear:(BOOL)animated
    {

    NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd-HH-mm-ss"];
    self.currentTime = [formatter stringFromDate:[NSDate date]];
    NSLog(@"DATE = %@", [NSDate date]);

    NSLog(@"ooooo%@", self.currentTime);

}

  • (void)viewWillDisappear:(BOOL)animated { NSDateFormatter *date=[[NSDateFormatter alloc] init]; [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *currentEndTime = [date stringFromDate:[NSDate date]]; NSCalendar *cal=[NSCalendar currentCalendar]; unsigned int unitFlags= NSYearCalendarUnit| NSMonthCalendarUnit| NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit; NSDateComponents *d = [cal components:unitFlags fromDate:self.currentTime toDate:currentEndTime options:0]; NSLog(@"%d天%d小時%d分鐘%d秒",[d day],[d hour],[d minute],[d second]);

}

打印

015-04-27 13:47:36.082 H_J_4[30420:576015] -[__NSCFString timeIntervalSinceReferenceDate]: unrecognized selector sent to instance 0x7fd6c144ebd0
(lldb)

最佳回答:


ios計算時間差 轉
//模擬windows下 GetTickCount //
//#import

  • (int)mxGetTickCount;
    {
    int timeTick;
    mach_timebase_info_data_t info;
    uint64_t machineTime;
    mach_timebase_info(&info);
    machineTime =mach_absolute_time();
    timeTick = machineTime * info.numer / info.denom /1000000LL;

    return timeTick;
    }

// NSDate timeIntervalSinceDate:

//下面轉換NSString,用在保存時間時使用。

  • (double)GetStringTimeDiff:(NSString*)timeS timeE:(NSString*)timeE

{

double timeDiff = 0.0;

NSDateFormatter *formatters = [[NSDateFormatter alloc] init];
[formatters setDateFormat:@"yyyy/MM/dd HH:mm:ss:SSS"];
NSDate *dateS = [formatters dateFromString:timeS];



NSDateFormatter *formatterE = [[NSDateFormatter alloc] init];
[formatterE setDateFormat:@"yyyy/MM/dd HH:mm:ss:SSS"];
NSDate *dateE = [formatterE dateFromString:timeE];


timeDiff = [dateE timeIntervalSinceDate:dateS ];

return timeDiff;

}

  • (NSString*)GetCurTime

{

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

[formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss:SSS"];

NSString*timeString=[formatter stringFromDate: [NSDate date]];

[formatter release];

return timeString;

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved