iOS 當前時間距離過去某個時間點的秒數

2022-05-12 14:52:34 字數 788 閱讀 6589

做藍芽開發的時候要設定同步時間,以秒為單位,當前時間距離2000-01-01 00:00:00的秒數。網上搜了好久的資料都沒找到合適的,然後研究了一下。具體寫法如下:

- (nstimeinterval)get2000yeartonowsecond

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

[dateformatter setdateformat:@"yyyy-mm-dd hh:mm:ss"];  // 設定時間格式

nstimezone *timezone = [nstimezone timezonewithname:@"gmt"];

[dateformatter settimezone:timezone]; //設定時區 +8:00

nsstring  *somedaystr= @"2000-01-01 00:00:00";   // 設定過去的某個時間點比如:2000-01-01 00:00:00

nsdate *somedaydate = [dateformatter datefromstring:somedaystr]; 

nsdate *currentdate = [nsdate date];

nstimeinterval time=[currentdate timeintervalsincedate:somedaydate];  //當前時間距離2000-01-01 00:00:00的秒數

nslog(@"%lld",(long long int)time);

return time;

Mysql計算時間距離

1,首先,看看mysql幾個內建時間函式 select now current timestamp current time sysdate 結果如下 2,計算時間差的內建函式timestampdiff timestampdiff interval,datetime expr1,datetime e...

iOS判斷當前時間是否在某個時間段

if self isbetweenfromhour 9 tohour 10 brief 判斷當前時間是否在fromhour和tohour之間。如,fromhour 8,tohour 23時,即為判斷當前時間是否在8 00 23 00之間 bool isbetweenfromhour nsintege...

時間當前 未來 過去 星期

時間格式 nsdateformatter formatter nsdateformatter alloc init 設定輸出格式 小時的 hh與hh 表示24小時制和 12小時制 formatter setdateformat yyyy mm dd hh mm ss 如果加上星期幾 則這樣 form...