How to get the zero hour (midnight) of a timestamp?

This timestamp (1609574624) corresponds to January 2, 2021 8:03:44 (check https://www.epochconverter.com/)

I need to reduce this timestamp to hour 0: January 2, 2021 0:00:00

How to do that?

You could use DateTime to do this.

Anyone?


local ts = 1609574624
local date = os.date("!*t", ts)
ts = os.time({year = date.year, month = date.month, day = date.day})
print(ts) --1609588800
date = os.date("%c", ts)
print(date) --Sat Jan  2 04:00:00 2021

‘!*t’ uses UTC.

1 Like
local TSOrig = 1609574624
print(TSOrig)
print('Orig:', DateTime.fromUnixTimestamp(TSOrig):FormatUniversalTime('L LT', 'en'))
local TimeTable = os.date("*t", TSOrig)
print(TimeTable)
local TS0 = os.time{year=TimeTable.year, month=TimeTable.month, day=TimeTable.day, hour=0, min = 0, sec=0}
print(TS0)
print('Result:', DateTime.fromUnixTimestamp(TS0):FormatUniversalTime('L LT', 'en'))

1609574624
Orig: 01/02/2021 8:03 AM
▼ {
[“day”] = 2,
[“hour”] = 5,
[“isdst”] = false,
[“min”] = 3,
[“month”] = 1,
[“sec”] = 44,
[“wday”] = 7,
[“yday”] = 2,
[“year”] = 2021
}
1609545600
Result: 01/02/2021 12:00 AM