Page 1 of 1

How can you get the current OS time?

Posted: Wed Oct 19, 2016 4:08 am
by knuxyl
Is there a way to get the operating systems time that is cross platform? (ie Mac,Windows,Linux,Android)

Re: How can you get the current OS time?

Posted: Wed Oct 19, 2016 4:52 am
by Ikroth
os.date, or os.time, depending on what you need.

https://www.lua.org/pil/22.1.html

Code: Select all

os.time() => 1476852730 -- seconds since epoch
os.date("%X", os.time()) => 00:50:02
os.date("%c", os.time()) => "Wed Oct 19 00:50:02 2016"
Edit: I don't know if these are cross-platform, but I haven't encountered any issues with them. Additionally, the results will conform to the current locale.

Re: How can you get the current OS time?

Posted: Wed Oct 19, 2016 5:23 am
by ivan
The timestamp IS platform independent - it counts the number of seconds since a specific point in time (1970),
whereas a 'readable' time string like "00:50:02" or "Wed Oct 19 00:50:02 2016" can be affected by timezone offsets and daylight savings.

Re: How can you get the current OS time?

Posted: Tue Sep 17, 2024 4:15 pm
by darkfrei
Is it possible to write date/time according to ISO 8601? For example date: 2024-09-17 or date and time: 2024-09-17T18:16:00Z

Re: How can you get the current OS time?

Posted: Tue Sep 17, 2024 7:17 pm
by pgimeno
Sure.

Code: Select all

print(os.date('%Y-%m-%d')) -- local date
print(os.date('!%Y-%m-%d')) -- zulu date
print(os.date('%Y-%m-%dT%H:%M:%S%z')) -- local time + time offset
print(os.date('!%Y-%m-%dT%H:%M:%SZ')) -- zulu time