Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get weekday for date value

76 views
Skip to first unread message

moonhkt

unread,
Jun 12, 2008, 11:51:16 PM6/12/08
to
Hi All

How to get weekday for date value using awk

e.g. Input 12/06/08, get FRI

Moonhk

Grant

unread,
Jun 13, 2008, 12:07:40 AM6/13/08
to

Dunno about getting Friday from Thursday's date ;)

~$ echo "13/06/08" | awk '{split($0,a,"/");print \
toupper(strftime("%a", mktime("20"a[3]" "a[2]" "a[1]" 12 0 0")))}'
FRI

Grant.
--
http://bugsplatter.mine.nu/

loki harfagr

unread,
Jun 13, 2008, 3:39:03 AM6/13/08
to
On Fri, 13 Jun 2008 14:07:40 +1000, Grant wrote:

> On Thu, 12 Jun 2008 20:51:16 -0700 (PDT), moonhkt <moo...@gmail.com>
> wrote:
>
>>Hi All
>>
>>How to get weekday for date value using awk
>>
>>e.g. Input 12/06/08, get FRI
>>
> Dunno about getting Friday from Thursday's date ;)

Ah, dire conditions may shrink time, hence you'll have
to use a cheat, here's one usink awk, a bunch of "ones"
and awkwardness ;-)

$ date -d $(echo "12/06/08" | awk 'NR==1{$1=1+$1} 1' RS=/ | tac | awk '1' ORS=) +%a
Fri

Aor if you insist on needing upper case:
$ date -d $(echo "12/06/08" | awk 'NR==1{$1=1+$1} 1' RS=/ | tac | awk '1' ORS=) +%^a
FRI

a mighty 'Par 5'...
at least it can be a showcase starter for the toolbox ;D)

Hermann Peifer

unread,
Jun 13, 2008, 4:01:58 AM6/13/08
to
On Jun 13, 9:39 am, loki harfagr <l...@theDarkDesign.free.fr> wrote:
> On Fri, 13 Jun 2008 14:07:40 +1000, Grant wrote:
> > On Thu, 12 Jun 2008 20:51:16 -0700 (PDT), moonhkt <moon...@gmail.com>

> > wrote:
>
> >>Hi All
>
> >>How to get weekday for date value using awk
>
> >>e.g. Input 12/06/08, get FRI
>
> > Dunno about getting Friday from Thursday's date ;)
>
>  Ah, dire conditions may shrink time, hence you'll have
> to use a cheat, here's one usink awk, a bunch of "ones"
> and awkwardness ;-)
>

Another cheat:

$ echo "12/06/08" | awk '{split($0,a,"/");print \
toupper(strftime("%a", mktime("20"a[3]" "a[2]" "a[1]" 24 0 0")))}'
FRI

Hermann

us...@domain.invalid

unread,
Jun 13, 2008, 5:30:29 AM6/13/08
to
Hermann Peifer escribió:

And there is also a pure AWK solution that do not need strftime, but
directly implements the Zeller's congruence algorithm:

function weekday( year, month, day, x1, x2, x3, x4 ) {
x1 = int( (14 - month) / 12 )
x2 = year - x1
x3 = month + 12 * x1 - 2
x4 = x2 + int(x2/4) - int(x2/100) + int(x2/400) + int( (31*x3) / 12)
return (x4 + day) % 7
}

--
Manuel Collado - http://lml.ls.fi.upm.es/~mcollado

0 new messages