iOS之獲取當前時間日期並按固定格式顯示

2022-08-01 12:24:12 字數 802 閱讀 6836

寫乙個常用的獲取當前日期,時間的**。並且能以規定的格式顯示出來12

345nsdate *currentdate = [nsdate date];//獲取當前時間,日期

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

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

nsstring *datestring = [dateformatter stringfromdate:currentdate];

nslog(@"datestring:%@",datestring);

這樣就能按照規定的格式顯示日期,時間了。

1datestring:2015/01/03 08:01:20 24

說明下格式對應的意義

yyyy(年)/mm(月)/dd(日) hh(時):mm(分):ss(秒) ss(毫秒)

需要用哪個的話就把哪個格式加上去。

值得注意的是,如果想顯示兩位數的年份的話,可以用」yy/mm/dd hh:mm:ss ss」,兩個y代表兩位數的年份。

而且大寫的m和小寫的m代表的意思也不一樣。「m」代表月份,「m」**分鐘

「hh」代表24小時制,「hh」代表12小時制

JS獲取當前時間日期

var mydate new date mydate.getyear 獲取當前年份 2位 mydate.getfullyear 獲取完整的年份 4位,1970 mydate.getmonth 獲取當前月份 0 11,0代表1月 mydate.getdate 獲取當前日 1 31 mydate.get...

iOS 獲取當前時間,並按照要求格式顯示

寫乙個常用的獲取當前日期,時間的 並且能以規定的格式顯示出來 1 2 3 4 5 nsdate currentdate nsdate date 獲取當前時間,日期 nsdateformatter dateformatter nsdateformatter alloc init dateformatt...

Qt獲取當前時間日期或時間戳QDateTime

獲取系統時間 qdatetime timecur qdatetime currentdatetime 獲取系統現在的時間 設定顯示格式 qstring timestr timecur.tostring yyyy mm dd hh mm ss ddd 秒 設定顯示格式 qstring timestr ...