day-of-week algorithm

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

day-of-week algorithm

Dave Collins

My API delivers dates in the form yyyy-mm-dd but my design team wants DDD dd (i.e. Tue 14).

 

Can anyone point me at a reliable algorithm for calculating day-of-week from a standard datestamp?

 

Dave Collins

Front-End Engineer

Mercatus Technologies Inc.
60 Adelaide Street East, Suite 700
Toronto ON M5C 3E4
T  416 603 3406 x 298
F  416 603 1790

 

[hidden email]

www.mercatustechnologies.com

 

Reply | Threaded
Open this post in threaded view
|

Re: day-of-week algorithm

Roger Burton West
On Tue, Jun 14, 2011 at 10:36:51AM -0400, Dave Collins wrote:
>Can anyone point me at a reliable algorithm for calculating day-of-week from a standard datestamp?

http://en.wikipedia.org/wiki/Calculating_the_day_of_the_week ?


Reply | Threaded
Open this post in threaded view
|

RE: day-of-week algorithm

Dave Collins
In reply to this post by Dave Collins

I guess as part of that, I’ll be wanting to convert yyyy-mm-dd into a table, like:

Year=yyyy

Month=mm

Day=dd

 

Is there a generic “split string into table with delimiter” method?

 

 

Dave Collins

Front-End Engineer

Mercatus Technologies Inc.
60 Adelaide Street East, Suite 700
Toronto ON M5C 3E4
T  416 603 3406 x 298
F  416 603 1790

 

[hidden email]

www.mercatustechnologies.com

 

From: Dave Collins
Sent: Tuesday, June 14, 2011 10:37 AM
To: '[hidden email]'
Subject: day-of-week algorithm

 

My API delivers dates in the form yyyy-mm-dd but my design team wants DDD dd (i.e. Tue 14).

 

Can anyone point me at a reliable algorithm for calculating day-of-week from a standard datestamp?

 

Dave Collins

Front-End Engineer

Mercatus Technologies Inc.
60 Adelaide Street East, Suite 700
Toronto ON M5C 3E4
T  416 603 3406 x 298
F  416 603 1790

 

[hidden email]

www.mercatustechnologies.com

 

Reply | Threaded
Open this post in threaded view
|

Re: day-of-week algorithm

Luiz Henrique de Figueiredo
In reply to this post by Dave Collins
> My API delivers dates in the form yyyy-mm-dd but my design team wants DDD dd (i.e. Tue 14).

stamp="2011-06-14"
local y,m,d=stamp:match("(%d+)%-(%d+)%-(%d+)")
local t={year=y, month=m, day=d}
print(os.date("%a %d",os.time(t)))


Reply | Threaded
Open this post in threaded view
|

Re: day-of-week algorithm

Tony Finch
In reply to this post by Dave Collins
Dave Collins <[hidden email]> wrote:

> My API delivers dates in the form yyyy-mm-dd but my design team wants
> DDD dd (i.e. Tue 14). Can anyone point me at a reliable algorithm for
> calculating day-of-week from a standard datestamp?

You can do it with Lua's os.date and os.time but this is more fun:

        local int = math.floor

        -- turn a Gregorian date into an RD number
        -- as defined in Calendrical Calculations
        -- Mon 0001-01-01 is RD 1
        -- Wed 1858-11-17 is RD 678576 and MJD 0
        -- Thu 1970-01-01 is RD 719163
        function rd(y,m,d)
                if m > 2
                then m = m + 1
                else m = m + 13; y = y - 1
                end
                return int(y*1461/4) - int(y/100) + int(y/400)
                     + int(m*153/5) + d - 428
        end

        local days = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }
        function dow(rd)
                return days[rd % 7 + 1]
        end

Tony.
--
f.anthony.n.finch  <[hidden email]>  http://dotat.at/
Forth, Tyne, Dogger: Southerly or southwesterly 3 or 4, increasing 5 at times.
Slight or moderate. Fair. Good.

Reply | Threaded
Open this post in threaded view
|

Re: day-of-week algorithm

Paul E. Merrell, J.D.
In reply to this post by Dave Collins
On Tue, Jun 14, 2011 at 7:36 AM, Dave Collins
<[hidden email]> wrote:
>
> My API delivers dates in the form yyyy-mm-dd but my design team wants DDD dd (i.e. Tue 14).
>
> Can anyone point me at a reliable algorithm for calculating day-of-week from a standard datestamp?

You might take a look through my Lua date/time resource notes,
attached. You might find what you're looking for in one of the linked
web pages.

Best regards,

Paul

Lua_Date-Time_Resources.html (23K) Download Attachment