Get the day of the week from a year, month and day

Given a string in the form 2010-05-22, this routine returns the day of the week as a number from 0 to 6, with 0 being Sunday.
use Time::Local;

sub get_weekday
    my ($date) = @_;
    my ($year, $mon, $mday) = split /-/, $date;
    my $time = timegm (0, 0, 0, $mday, $mon, $year);
    my @dates = gmtime ($time);
    my $wday = $dates[6];
    return $wday;

Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock ( or use the discussion group at Google Groups. / Privacy / Disclaimer