iOS開發之計算兩個日期的時間間隔

2022-08-19 00:39:52 字數 852 閱讀 3992

//首先建立格式化物件

nsdateformatter *dateformatter = [[nsdateformatter alloc] init];

[dateformatter setdateformat:@"yyyy-mm-dd hh:mm:ss"];

//然後建立日期物件   

nsdate *date1 = [dateformatter datefromstring:@"2020-10-31 00:00:00"];

nsdate *date = [nsdate date];

//計算時間間隔(單位是秒)

nstimeinterval time = [date1 timeintervalsincedate:date];

//計算天數、時、分、秒

int days = ((int)time)/(3600*24);

int hours = ((int)time)%(3600*24)/3600;

int minutes = ((int)time)%(3600*24)%3600/60;

int seconds = ((int)time)%(3600*24)%3600%60;

nsstring *datecontent = [[nsstring alloc] initwithformat:@"僅剩%i天%i小時%i分%i秒",days,hours,minutes,seconds];

(%i可以自動將輸入轉換為十進位制,而%d則不會進行轉換)

//賦值顯示

uilabel *timelab = (uilabel *)[self.view viewwithtag:666666];

timelab.text = datecontent;

計算兩個日期的時間間隔

計算兩個日期的時間間隔 第乙個日期和時間 第二個日期和時間 private string datediff datetime datetime1,datetime datetime2 說明 1.datetime 值型別代表了乙個從公元 0001年1 月1日0 點0分0 秒到公元 9999年12 月3...

演算法筆記(一) 之計算兩個日期之差

老規矩 妹妹鎮樓 輸入格式 yyyymmdd 20130101 20130505 輸出個數 5 要計算日期之間的天數,我們可以想到從小的日期開始不斷加1天,直到小的日期等於大的日期為止。其中,當天數d等於當前的月份m的天數 1時,月份m就要 1,且天數d置為1 當月份m變為13時,則年份y就要 1,...

php兩個日期時間差 用php計算兩個日期相差天數

用php計算兩個日期相差天數 php計算兩個日期相差天數的 方法,例項分析了php操作日期的技巧,需要的朋友可以參考下。求兩個日期之間相差的天數 針對1970年1月1日之後,求之前可以採用泰勒公式 param string day1 param string day2 return number f...