Garry's Mod Wiki

Revision Difference

os.date#526472

<function name="date" parent="os" type="libraryfunc"> <description>Returns the date/time as a formatted string or in a table.</description> <realm>Shared and Menu</realm> <args> <arg name="format" type="string">The format string. If this is equal to '*t' or '!*t' then this function will return a <page>Structures/DateData</page>, otherwise it will return a string. If this starts with an '!', the returned data will use the UTC timezone rather than the local timezone. See http://www.mkssoftware.com/docs/man3/strftime.3.asp for available format flags. <bug issue="329">**Not all flags are available on all operating systems** and the result of using an invalid flag is undefined. This currently crashes the game on Windows. Most or all flags are available on OS X and Linux but considerably fewer are available on Windows. See http://msdn.microsoft.com/en-us/library/fe06s4ak.aspx for a list of available flags on Windows. Note that the **#** flags also crashes the game on Windows.</bug></arg>⤶ <arg name="time" type="number">Time to use for the format.</arg>⤶ <bug issue="329">**Not all flags are available on all operating systems** and the result of using an invalid flag is undefined. This currently crashes the game on Windows. Most or all flags are available on OS X and Linux but considerably fewer are available on Windows. See http://msdn.microsoft.com/en-us/library/fe06s4ak.aspx for a list of available flags on Windows. Note that the **#** flags also crashes the game on Windows.</bug>⤶ ⤶ Known formats that work on all platforms:⤶ ⤶ | Format | Description | Example of the output |⤶ |:------:|:-----------:|:---------------------:|⤶ | %a | Abbreviated weekday name | `Wed` |⤶ | %A | Full weekday name | `Wednesday` |⤶ | %b | Abbreviated month name | `Sep` |⤶ | %B | Full month name | `September` |⤶ | %c | Date and time | `09/16/98 23:48:10` |⤶ | %d | Day of the month [01-31] | `16` |⤶ | %H | Hour, using a 24-hour clock [00-23] | `23` |⤶ | %I | Hour, using a 12-hour clock [01-12] | `11` |⤶ | %j | Day of the year [001-365] | `259` |⤶ | %m | Month [01-12] | `09` |⤶ | %M | Minute [00-59] | `48` |⤶ | %p | Either `am` or `pm` | `pm` |⤶ | %S | Second [00-60] | `10` |⤶ | %w | Weekday [0-6 = Sunday-Saturday] | `3` |⤶ | %W | Week of the year [00-53] | `37` |⤶ | %x | Date | `09/16/98` |⤶ | %X | Time | `24:48:10` |⤶ | %y | Two-digit year [00-99] | `98` |⤶ | %Y | Full year | `1998` |⤶ | %z | Timezone | `-0300` |⤶ | %% | A percent sign | `%` |⤶ ⤶ ⤶ </arg>⤶ <arg name="time" type="number" default="os.time()">Time to use for the format.</arg>⤶ </args> <rets> <ret name="" type="string">Formatted date <note>This will be a <page>Structures/DateData</page> if the first argument equals to '*t' or '!*t'</note></ret> </rets> </function> <example> <description> This will use the os.time() function, and return it in a friendly way. os.time() is useful for storing as a date stamp but needs this to make it readable. </description> <code> local Timestamp = os.time() local TimeString = os.date( "%H:%M:%S - %d/%m/%Y" , Timestamp ) print( "Timestamp:", Timestamp ) print( "TimeString:", TimeString ) </code> <outputfixedwidth>Fixed width</outputfixedwidth> <output> Timestamp: 1584402168 <br/> TimeString: 23:42:48 - 16/03/2020 </output> </example>