os.date

From ComputerCraft
Jump to navigation Jump to search

os.date
Function
Syntax
os.date()
Returns string text
API os
Source CC:Tweaked

Returns a formatted date and time string, using format as a formatting string. It will format the current time, unless you pass a Unix timestamp (in seconds) to unixtime.

If the format starts with !, then the date will be formatted in UTC. After this, if the string starts with *t, then the date will return a table instead of a formatted string containing the following values:

os.date("*t") table fields
Name Field Type Range Example Notes
Year year number 4 digits 2020
Month month number 1–12 6
Day of the month day number 1–31 3
Hour hour number 0–23 18
Minute min number 0–59 54
Second sec number 0–59[1] 44 Ignores leap seconds[1]
Weekday wday number 1–7 4 Sunday is 1
Day of the year yday number 1–366 155 1–365 unless leap year
Is DST? isdst boolean true/false false
  1. In CC:Tweaked, the seconds range does not account for leap seconds, unlike regular Lua, so the range is 0–59. Also note that the Lua manual incorrectly refers to this range as 0–61, whereas it should be 0–60.


Otherwise, a custom formatting string can be created using the following specifiers:

os.date() formatting fields (strftime-based) (source)
Code Description Range Example (UTC) Notes
Year
%Y Year 0000–9999 2020 4-digit zero-padded
%y Year 00–99 20 2-digit zero-padded
%G ISO 8601 week-based year 0000–9999 2020 4-digit zero-padded
%g ISO 8601 week-based year 00–99 20 2-digit zero-padded
%C Century 00–99 20 Starts at 0 (so 2020 gives 20)
Month
%m Month 01–12 06 2-digit zero-padded, starts at 1
%B Full month name January–December June
%b, %h Abbreviated month name Jan–Dec Jun
Day of the month or the year
%d Day of the month 01–31 03 2-digit zero-padded
%e Day of the month  1–31 03 2-digit space-padded
%j Day of the year 01–12 155 3-digit zero-padded, starts at 1
Week and day of the week
%A Full weekday name Sunday–Saturday Wednesday
%a Abbreviated weekday name Sun–Sat Wed
%u Weekday 1–7 4 Monday is 1 (unlike wday)
%w Weekday 0–6 3 Monday is 0
%V Week of ISO 8601 week-based year 01–53 23
%U Week of year 00–53 22 First Sunday is first day of week 01
(example column may be incorrect)
%W Week of year 00–53 22 First Monday is first day of week 01
(example column may be incorrect)
Hour
%H Hour of day (24-hour) 00–23 18 2-digit zero-padded 24-hour, starts at 0
%I Hour of day (12-hour) 01–12 06 2-digit zero-padded 12-hour, starts at 1/12
%p AM/PM AM/PM PM Uppercase only (unlike strftime)
Minutes and seconds
%M Minute 00–59 54 2-digit zero-padded
%S Second 00–59[1] 44 2-digit zero-padded, ignores leap seconds[1]
Predefined date and time formats
%c Date and time N/A Wed Jun 3 18:54:44 2020 Abbreviated space-padded dates,
2-digit zero-padded 24-hour times
%D, %x Date (US format) N/A 06/03/20 Month/day/year, 2-digit zero-padded
%F Date (ISO 8601) N/A 2020-06-03 Year-month-day
%r Time N/A 06:54:44 PM 2-digit zero-padded 12-hour times with AM/PM
%R Time N/A 18:54 2-digit zero-padded 24-hour, no seconds
%T, %X Time N/A 18:54:44 2-digit zero-padded 24-hour times
Timezone
%Z Timezone name or abbreviation N/A BST Behaviour depends on appendChronologyId
%z Timezone offset N/A +0000 Difference to GMT in hours and minutes (±HHMM)
Escape codes
%n Literal newline N/A \n
%t Literal tab N/A \t
%% Literal percent N/A %
ExampleExamplePrint time and date
Prints the current time and date.
Code
print(os.date("%d.%m.%Y %H:%M"))
Output
13.02.2020 18:00