日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

lua os.date函数定义和示例

發布時間:2025/3/8 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 lua os.date函数定义和示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

os.date函數定義

  • 原型:os.date ([format [, time]])
  • 解釋:返回一個按format格式化日期、時間的字串或表。

lua源碼中os.date的注釋如下:

--- --- Returns a string or a table containing date and time, formatted according --- to the given string `format`. --- --- If the `time` argument is present, this is the time to be formatted (see --- the `os.time` function for a description of this value). Otherwise, --- `date` formats the current time. --- --- If `format` starts with '`!`', then the date is formatted in Coordinated --- Universal Time. After this optional character, if `format` is the string --- "`*t`", then `date` returns a table with the following fields: --- --- **`year`** (four digits) --- **`month`** (1–12) --- **`day`** (1-31) --- **`hour`** (0-23) --- **`min`** (0-59) --- **`sec`** (0-61), due to leap seconds --- **`wday`** (weekday, 1–7, Sunday is 1) --- **`yday`** (day of the year, 1–366) --- **`isdst`** (daylight saving flag, a boolean). This last field may be absent --- if the information is not available. --- --- If `format` is not "`*t`", then `date` returns the date as a string, --- formatted according to the same rules as the ISO C function `strftime`. --- --- When called without arguments, `date` returns a reasonable date and time --- representation that depends on the host system and on the current locale. --- (More specifically, `os.date()` is equivalent to `os.date("%c")`.) --- --- On non-POSIX systems, this function may be not thread safe because of its --- reliance on C function `gmtime` and C function `localtime`. ---@overload fun():string|table ---@param format string ---@param time number ---@return string|table function os.date(format, time) end

os.date格式符對照表

os.date ([format [, time]])

由原型可以看出可以省略第二個參數也可以省略兩個參數,

只省略第二個參數函數會使用當前時間作為第二個參數,

如果兩個參數都省略則按當前系統的設置返回格式化的字符串,做以下等價替換 os.date() <=> os.date("%c")。

如果format以 “!” 開頭,則按格林尼治時間進行格式化。

如果format是一個 “t” ,將返一個帶year(4位),month(1-12), day (1--31), hour (0-23), min (0-59),sec (0-61),wday (星期幾, 星期天為1), yday (年內天數)和isdst (是否為日光節約時間true/false)的帶鍵名的表;

如果format不是 “t” ,os.date會將日期格式化為一個字符串,具體如下:

格式符含義具體示例
%a一星期中天數的簡寫os.date("%a") => Fri
%A一星期中天數的全稱(Wednesday)
%b月份的簡寫(Sep)
%B月份的全稱(May)
%c日期和時間(09/16/98 23:48:10)
%d一個月中的第幾天(28)[0 - 31]
%H24小時制中的小時數(18)[00 - 23]
%I12小時制中的小時數(10)[01 - 12]
%j一年中的第幾天(209) [01 - 366]
%M分鐘數(48)[00 - 59]
%m月份數(09)[01 - 12]
%P上午或下午(pm)[am - pm]
%S一分鐘之內秒數(10)[00 - 59]
%w一星期中的第幾天(3)[0 - 6 = 星期天 - 星期六]
%W一年中的第幾個星期(2)0 - 52
%x日期(09/16/98)
%X時間(23:48:10)
%y兩位數的年份(16)[00 - 99]
%Y完整的年份(2016)
%%字符串'%'(%)
*t返回一個table,里面包含全部的數據hour 14

使用示例

我的lua版本:lua5.3

print ("os.date(\"*t\") 示例:\n") local timetable = os.date("*t", os.time()); for i, v in pairs(timetable) doprint(i, v); endprint ("\n \"!\" 開頭:\n") local utimetable = os.date("!*t", os.time()); for i, v in pairs(utimetable) doprint(i, v); endprint ("\n其它用法:\n") print(os.date("今天是 %c, 星期 %A"))

輸出結果:

今天東八區中國廣州的日期為:2018-11-1 21:13 星期四

os.date("*t") 示例: wday 5 year 2018 day 1 sec 51 hour 21 isdst false month 11 yday 305 min 13 os.date("!*t") 示例: wday 5 year 2018 day 1 sec 38 hour 13 --- 差8小時 isdst false month 11 yday 305 min 14 今天是 11/01/18 21:15:36, 星期 Thursday
  • 注意format "!" 的用法,因為我們的時間(北京)處于東8區,所以兩次的結果會差8個小時(13+8=21),從結果中可以看出。
  • 注意使用format "*t"返回的table中wday如果是1表示星期天,而使用通用格式時%w用0表示星期天。

參考:https://www.jianshu.com/p/76ac11863591

轉載于:https://www.cnblogs.com/zhaoqingqing/p/9892694.html

總結

以上是生活随笔為你收集整理的lua os.date函数定义和示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。