I don’t want to use DateTime since it’s based on the client’s clock, which can be tampered with.
I’d like to get the time (January 2nd 2020 at 10:30 AM, 45 seconds) in this format, super accurately, that can’t be tampered with: 2020-01-02T10:30:45Z
Thanks!
Hello, this can be done pretty easily (assuming you’re trying to do it from the client), even with DateTime
objects by using workspace:GetServerTimeNow()
. The GetServerTimeNow
method basically gets the current epoch time in seconds from the server (to the millisecond+) and accounts for any ping delays that may occur when fetching. Using this method, you can just create a DateTime
object with the fetched time and format it from there. Here’s what it would look like in code:
local dateTimeObject = DateTime.fromUnixTimestamp(workspace:GetServerTimeNow())
local formatted = dateTimeObject:FormatUniversalTime("YYYY-MM-DD\THH:mm:ss\Z","en-us")
print(formatted)
For information on how to do the formatting, see DateTime
’s page on the reference under the “String Format” section.
Hope this helps!
1 Like