Calculate day of the week for a given date MM-DD-YYYY

KevinADC 1 Tallied Votes 1K Views Share

Given the date in mm-dd-yyyy (or M-D-YYYY) format (or any combination of that format) you can find out what day of the week that was on or is on or will be on, within the limitations of the system the script resides on.

This snippet uses only core functions and modules so nothing needs to be installed but it is worth mentioning that there are numerous date modules on CPAN that offer a wide range of functions for handling date calculations.

http://search.cpan.org/search?query=date&mode=all

This snippet assumes the date is in mm-dd-yyyy format but can easily be adapted to handle any combination of those date parameters. Can also handle years in three digit and two digit format but see the Time::Local and localtime() documentation for limitations.

leo002000 commented: 3 +3
use Time::Local 'timelocal_nocheck';

# An array to hold the names of each day of the week. 
my @weekday = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);

# '09-11-2001' can be a parameter/argument you pass to the script
my $mm_dd_yyyy = '09-11-2001';
my $day_of_week = get_day($mm_dd_yyyy);
print "$mm_dd_yyyy was a $day_of_week";

sub get_day {
   my $date = shift || return(0); 
   my ($mon,$mday,$year) = $date =~ /(\d+)-(\d+)-(\d+)/;
   my $epochtime = timelocal_nocheck(0, 0, 0, $mday, $mon-1, $year);
   my $day = (localtime($epochtime))[6];
   return $weekday[$day];
}

Dani AI

Generated

Good, compact baseline from : the Time::Local + localtime approach is core-only and works for most everyday dates. A few practical caveats and small improvements make the solution more robust.

A short, explicit alternative in Python (core) that parses MM-DD-YYYY and returns the weekday name:

from datetime import datetime

def day_of_week(mmddyyyy):
    dt = datetime.strptime(mmddyyyy, "%m-%d-%Y")  # raises ValueError on bad dates
    return dt.strftime("%A")

print(day_of_week("09-11-2001"))  # Tuesday

Practical notes and improvements applicable to either language:

  • Validate input first (e.g. /^\d{1,2}-\d{1,2}-\d{2,4}$/) and reject malformed strings before converting.
  • Decide how to treat two-digit years explicitly; implicit heuristics lead to surprises.
  • Timezone and epoch: localtime depends on the host timezone and DST rules. For timezone-independent weekday calculation use UTC-based functions (timegm/timegm_nocheck in Perl) or an explicit UTC DateTime object.
  • Range and accuracy: many lightweight APIs depend on the system time_t range (the 2038 issue on 32-bit builds). For historical or very-future dates prefer DateTime, Date::Calc, or Time::Piece in Perl.
  • Test edge cases: 02-29 on leap years, century boundaries (1900, 2000), and DST transitions.

Keep parsing, validation, and weekday logic separate and cover the important edge cases with unit tests for reliable results.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.