经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 游戏设计 » 查看文章
lua os.date函数定义和示例
来源:cnblogs  作者:赵青青  时间:2018/12/26 9:32:24  对本文有异议

os.date函数定义

  • 原型:os.date ([format [, time]])
  • 解释:返回一个按format格式化日期、时间的字串或表。

lua源码中os.date的注释如下:

  1. ---
  2. --- Returns a string or a table containing date and time, formatted according
  3. --- to the given string `format`.
  4. ---
  5. --- If the `time` argument is present, this is the time to be formatted (see
  6. --- the `os.time` function for a description of this value). Otherwise,
  7. --- `date` formats the current time.
  8. ---
  9. --- If `format` starts with '`!`', then the date is formatted in Coordinated
  10. --- Universal Time. After this optional character, if `format` is the string
  11. --- "`*t`", then `date` returns a table with the following fields:
  12. ---
  13. --- **`year`** (four digits)
  14. --- **`month`** (112)
  15. --- **`day`** (1-31)
  16. --- **`hour`** (0-23)
  17. --- **`min`** (0-59)
  18. --- **`sec`** (0-61), due to leap seconds
  19. --- **`wday`** (weekday, 17, Sunday is 1)
  20. --- **`yday`** (day of the year, 1366)
  21. --- **`isdst`** (daylight saving flag, a boolean). This last field may be absent
  22. --- if the information is not available.
  23. ---
  24. --- If `format` is not "`*t`", then `date` returns the date as a string,
  25. --- formatted according to the same rules as the ISO C function `strftime`.
  26. ---
  27. --- When called without arguments, `date` returns a reasonable date and time
  28. --- representation that depends on the host system and on the current locale.
  29. --- (More specifically, `os.date()` is equivalent to `os.date("%c")`.)
  30. ---
  31. --- On non-POSIX systems, this function may be not thread safe because of its
  32. --- reliance on C function `gmtime` and C function `localtime`.
  33. ---@overload fun():string|table
  34. ---@param format string
  35. ---@param time number
  36. ---@return string|table
  37. 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]
%H 24小时制中的小时数 (18)[00 - 23]
%I 12小时制中的小时数 (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

  1. print ("os.date(\"*t\") 示例:\n")
  2. local timetable = os.date("*t", os.time());
  3. for i, v in pairs(timetable) do
  4. print(i, v);
  5. end
  6. print ("\n \"!\" 开头:\n")
  7. local utimetable = os.date("!*t", os.time());
  8. for i, v in pairs(utimetable) do
  9. print(i, v);
  10. end
  11. print ("\n其它用法:\n")
  12. print(os.date("今天是 %c, 星期 %A"))

输出结果:

今天东八区中国广州的日期为:2018-11-1 21:13 星期四

  1. os.date("*t") 示例:
  2. wday 5
  3. year 2018
  4. day 1
  5. sec 51
  6. hour 21
  7. isdst false
  8. month 11
  9. yday 305
  10. min 13
  1. os.date("!*t") 示例:
  2. wday 5
  3. year 2018
  4. day 1
  5. sec 38
  6. hour 13 --- 8小时
  7. isdst false
  8. month 11
  9. yday 305
  10. min 14
  1. 今天是 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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号