Day Of Week

Is there someone who knows a good method for calculating the day of week from a date? 

Look at the documentation for os.date().  It might also be useful to Google “strftime” and check out this blog post:
 

http://www.coronalabs.com/blog/2013/01/15/working-with-time-and-dates-in-corona/

Thanks for the answer. After Googling for some time i found the solution. So I share it here with everybody in case someone reads this thread in the future looking for the same thing.

function get\_day\_of\_week(dd, mm, yy) local days = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" } local mmx = mm if (mm == 1) then mmx = 13; yy = yy-1 end if (mm == 2) then mmx = 14; yy = yy-1 end local val8 = dd + (mmx\*2) + math.floor(((mmx+1)\*3)/5) + yy + math.floor(yy/4) - math.floor(yy/100) + math.floor(yy/400) + 2 local val9 = math.floor(val8/7) local dw = val8-(val9\*7) if (dw == 0) then dw = 7 end return dw, days[dw] end

I came up with an easier method:

function get\_day\_of\_week( dd, mm, yy ) local timestamp = os.time( { year = 1900 + yy, month = mm, day = dd } ) return os.date( "%w", timestamp ) + 1, os.date( "%a", timestamp ) end

That one would have been really nice, but its off by one day. If you could fix it, id would really like that  :slight_smile:

Are you referring to the first returned argument being the day number of the week?  (Sunday = 1, Monday = 2, Tuesday = 3, … Saturday = 7).  You can remove the + 1 in the return statement so that the values are 0 to 6 instead 1 to 7.

Yeah I figured that out. :)  Thanks

Look at the documentation for os.date().  It might also be useful to Google “strftime” and check out this blog post:
 

http://www.coronalabs.com/blog/2013/01/15/working-with-time-and-dates-in-corona/

Thanks for the answer. After Googling for some time i found the solution. So I share it here with everybody in case someone reads this thread in the future looking for the same thing.

function get\_day\_of\_week(dd, mm, yy) local days = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" } local mmx = mm if (mm == 1) then mmx = 13; yy = yy-1 end if (mm == 2) then mmx = 14; yy = yy-1 end local val8 = dd + (mmx\*2) + math.floor(((mmx+1)\*3)/5) + yy + math.floor(yy/4) - math.floor(yy/100) + math.floor(yy/400) + 2 local val9 = math.floor(val8/7) local dw = val8-(val9\*7) if (dw == 0) then dw = 7 end return dw, days[dw] end

I came up with an easier method:

function get\_day\_of\_week( dd, mm, yy ) local timestamp = os.time( { year = 1900 + yy, month = mm, day = dd } ) return os.date( "%w", timestamp ) + 1, os.date( "%a", timestamp ) end

That one would have been really nice, but its off by one day. If you could fix it, id would really like that  :slight_smile:

Are you referring to the first returned argument being the day number of the week?  (Sunday = 1, Monday = 2, Tuesday = 3, … Saturday = 7).  You can remove the + 1 in the return statement so that the values are 0 to 6 instead 1 to 7.

Yeah I figured that out. :)  Thanks