IOS 獲取距離當前時間之後或之前的N天

2021-07-16 17:58:41 字數 668 閱讀 1243

nsinteger dis = 7; //前後的天數

nsdate*nowdate = [nsdate date];

nsdate* thedate;

nstimeinterval  oneday = 24*60*60*1;  //1天的長度

//之後的天數      

thedate = [nowdate initwithtimeintervalsincenow: +oneday*dis ];

//之前的天數

thedate = [nowdate initwithtimeintervalsincenow: -oneday*dis ];

//例項化乙個nsdateformatter物件

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

//設定時間格式,這裡可以設定成自己需要的格式

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

//用[nsdate date]可以獲取系統當前時間

nsstring * currentdatestr = [dateformatter stringfromdate:thedate];

nslog(@"%@",currentdatestr);

iOS 獲取當前時間和當前時間戳

獲取當前的時間 nsstring getcurrenttimes nsdateformatter formatter nsdateformatter alloc init 設定你想要的格式,hh與hh的區別 分別表示12小時制,24小時制 formatter setdateformat yyyy m...

iOS 獲取當前時間和當前時間戳

獲取當前的時間 nsstring getcurrenttimes nsdateformatter formatter nsdateformatter alloc init 設定你想要的格式,hh與hh的區別 分別表示12小時制,24小時制 formatter setdateformat yyyy m...

python之獲取當前時間

本文使用的python版本是3.8.1 2.32位 macos 指令 意義 y 十進位制數表示的帶世紀的年份 eg 2020 y十進位制數 00,99 表示的沒有世紀的年份 eg 20 m十進位制數 01,12 表示的月 d十進位制數 01,31 表示的哪一天 h十進位制數 00,23 表示的小時 ...