I've browsed through the Roblox wiki and I couldn't find anything that could help me. Is there any way I can get the current time and date from a server using HTTPService? Or maybe using the OS?
You can put something in serverscriptservice called time and put this inside of it.
1 | local TimeServer = "http://www.timeapi.org/utc/now?%25Q" |
2 | local CurrentTime = tonumber (Game:GetService( "HttpService" ):GetAsync(TimeServer, true )) |
3 | local ServerTime = tick() |
4 |
5 | return function () return (CurrentTime / 1000 ) - ServerTime + tick() end |
Then you could use it in a script like this.
1 | local getTime = require(Game.ServerScriptService.Time) |
2 |
3 | print (getTime()) |
4 | wait( 1 ) |
5 | print (getTime()) |
This uses the utc time zone, because roblox servers are run in different zones I used time api. If you are just trying to get how long it has been that will be fine but you can't get a player's current timezone.
The os.date function is useful for this. It can return a date table for either UTC time or local time.
Example usage:
1 | local date_table = os.date( "!*t" ) -- this is for UTC time, "*t" would be for local time |
2 | print (date_table.month) --> 5 |
3 | print (date_table.day) --> 26 |
More information, including what fields a date table has, can be found at: http://wiki.roblox.com/index.php?title=Global_namespace/Basic_functions#os.date https://www.lua.org/pil/22.1.html