Inverse os.date

os.date(%j) will give me the day of the year, from 1-366.

Is there a function that will give me the month for a specific day? (i.e. month(364) will give December)

No but you should be able to write that function yourself without much trouble. Remember to take leap years into account.

If you are using os.date() to get the day of year like you are doing, why not use one of the other options like %B to get the month?  Look at the strftime() (C library call that os.date is based on) formatting options:  http://www.cplusplus.com/reference/ctime/strftime/

Rob

If I wanted to get the current month, I could do that. I want to get the month in 45 days, which might be next month, or the month after that, depending on when I run the function. I didn’t want to write a function to do that if it already existed.

Ideally, I could get the current day of the year, add 45 to it, then get the month of (current day + 45). But now it’s just a little more involved.

Then you get the current time, add 45 days to it (45 * 24 * 60 * 60 since it’s in seconds) and use that value as the input for os.date().

Rob

So using the example from the documentation, if I do this:

currenttime = os.time()

futuretime = currenttime + (45 * 24* 60 *60)

futuremonth = os.date( %m,futuretime)

futuremonth will give me the month in 45 days as a number from 1-12?

futuremonth = os.date("%B", futuretime)

should produce the month string.  But even with the month number you could do a simple look up table:

local monthNames = { “Jan”,“Feb”, “Mar” … etc.}

print(monthNames[futureMonth]).

Rob

For the app, I need to produce a code based on the month, so an integer value is sufficient.

But that will work. Thanks for the help.

No but you should be able to write that function yourself without much trouble. Remember to take leap years into account.

If you are using os.date() to get the day of year like you are doing, why not use one of the other options like %B to get the month?  Look at the strftime() (C library call that os.date is based on) formatting options:  http://www.cplusplus.com/reference/ctime/strftime/

Rob

If I wanted to get the current month, I could do that. I want to get the month in 45 days, which might be next month, or the month after that, depending on when I run the function. I didn’t want to write a function to do that if it already existed.

Ideally, I could get the current day of the year, add 45 to it, then get the month of (current day + 45). But now it’s just a little more involved.

Then you get the current time, add 45 days to it (45 * 24 * 60 * 60 since it’s in seconds) and use that value as the input for os.date().

Rob

So using the example from the documentation, if I do this:

currenttime = os.time()

futuretime = currenttime + (45 * 24* 60 *60)

futuremonth = os.date( %m,futuretime)

futuremonth will give me the month in 45 days as a number from 1-12?

futuremonth = os.date("%B", futuretime)

should produce the month string.  But even with the month number you could do a simple look up table:

local monthNames = { “Jan”,“Feb”, “Mar” … etc.}

print(monthNames[futureMonth]).

Rob

For the app, I need to produce a code based on the month, so an integer value is sufficient.

But that will work. Thanks for the help.