Quick date lookup

Does anyone know of a quick way to find yesterdays date (for example) without creating a huge lookup table? The following will return todays month, date etc etc, but I need to know the date details for a previous day:

[lua]local date = os.date( “*t” ) – returns table of date & time values
print( date.year, date.month ) – print year and month
print( date.hour, date.min ) – print hour and minutes[/lua]

I’m trying to avoid creating epic if statements checking for name of month, days in month, is it a leap year etc etc… [import]uid: 173326 topic_id: 33677 reply_id: 333677[/import]

As far as i can tell Lua either doesn’t handle date functions well or it’s not documented well. In most cases where I needed to use date functions like adding/substracting days I was also using SQLITE. That being in the case i just used SQLite to handle my dates. If no one provides a pure Lua solution then that could be an option for ya. [import]uid: 147305 topic_id: 33677 reply_id: 133896[/import]

date = os.date("*t")
print( date.year, date.month, date.day )
date.day = date.day - 1
print( date.year, date.month, date.day ) [import]uid: 100222 topic_id: 33677 reply_id: 133902[/import]

Thanks for the pointers budershank. Shame there isn’t a more easy way than SQLITE. [import]uid: 173326 topic_id: 33677 reply_id: 133904[/import]

The problem with that is it treats date.day as a regular number. If it was the first then date.day would be zero. Change the -1 to -6 and you can see the issue. [import]uid: 147305 topic_id: 33677 reply_id: 133905[/import]

jeff.wesson - nope, that won’t work. Try subtracting 60 days like I’ve done below and you’ll see why it won’t work.

[lua]date = os.date("*t")
print( date.year, date.month, date.day )
date.day = date.day - 60
print( date.year, date.month, date.day )[/lua] [import]uid: 173326 topic_id: 33677 reply_id: 133906[/import]

This should work for what your trying to do.

[code]
local sDay = 86400 – number of seconds in a day
local t = os.date( ‘*t’ ) – get table of current date and time
local tnum = os.time( t ) – date & time as number of seconds --> 1287516614

tnum = tnum - sDay – 24 hours subtracted from current time
local newDate = os.date( “*t” , tnum) – table using the new updated date/time

print (newDate.month … “/” … newDate.day … “/” … newDate.year)
– this prints yesterday’s date e.g. 12/4/2012

[/code] [import]uid: 56820 topic_id: 33677 reply_id: 133918[/import]

Nice one anderoth!! That does the job perfectly! Thanks! [import]uid: 173326 topic_id: 33677 reply_id: 134032[/import]

As far as i can tell Lua either doesn’t handle date functions well or it’s not documented well. In most cases where I needed to use date functions like adding/substracting days I was also using SQLITE. That being in the case i just used SQLite to handle my dates. If no one provides a pure Lua solution then that could be an option for ya. [import]uid: 147305 topic_id: 33677 reply_id: 133896[/import]

date = os.date("*t")
print( date.year, date.month, date.day )
date.day = date.day - 1
print( date.year, date.month, date.day ) [import]uid: 100222 topic_id: 33677 reply_id: 133902[/import]

Thanks for the pointers budershank. Shame there isn’t a more easy way than SQLITE. [import]uid: 173326 topic_id: 33677 reply_id: 133904[/import]

The problem with that is it treats date.day as a regular number. If it was the first then date.day would be zero. Change the -1 to -6 and you can see the issue. [import]uid: 147305 topic_id: 33677 reply_id: 133905[/import]

jeff.wesson - nope, that won’t work. Try subtracting 60 days like I’ve done below and you’ll see why it won’t work.

[lua]date = os.date("*t")
print( date.year, date.month, date.day )
date.day = date.day - 60
print( date.year, date.month, date.day )[/lua] [import]uid: 173326 topic_id: 33677 reply_id: 133906[/import]

This should work for what your trying to do.

[code]
local sDay = 86400 – number of seconds in a day
local t = os.date( ‘*t’ ) – get table of current date and time
local tnum = os.time( t ) – date & time as number of seconds --> 1287516614

tnum = tnum - sDay – 24 hours subtracted from current time
local newDate = os.date( “*t” , tnum) – table using the new updated date/time

print (newDate.month … “/” … newDate.day … “/” … newDate.year)
– this prints yesterday’s date e.g. 12/4/2012

[/code] [import]uid: 56820 topic_id: 33677 reply_id: 133918[/import]

Nice one anderoth!! That does the job perfectly! Thanks! [import]uid: 173326 topic_id: 33677 reply_id: 134032[/import]