#!/usr/bin/perl -w require 5.003; use POSIX "floor"; @DoW = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'); @MoY=('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'); # For a leap year February needs to be corrected (done in Step 1). @MonthLength = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); # ------------------ get input --------------------- print "\n Give me a date and I'll tell you the day of the week. \n\tMonth? [1,...12] "; $Month = ; chomp($Month); print "\t\t (you picked $MoY[$Month-1])\n"; print "\n\tDay? "; $Day = ; chomp($Day); print "\n\tYear? [as: 2012] "; $Year = ; chomp($Year); # --------- main program begins here ---------------- if ( (($Year % 4) == 0) and ( (($Year % 100) != 0) or ( ($Year % 400) == 0) ) ) { $MonthLength[1]=29; } $Day_Num = 0; # Initialize for ($i=0; $i < ($Month-1); $i++) { # Days in preceding months $Day_Num = $Day_Num + $MonthLength[$i]; } $Day_Num = $Day_Num + $Day; # Add correction for current month. $Y1 = $Year - 1; # previous year $Day_of_Week = ($Day_Num + $Y1 + floor($Y1/4) - floor($Y1/100) + floor($Y1/400) ) % 7; $Answer = $DoW[$Day_of_Week]; print "\n==> $MoY[$Month-1] $Day, $Year is a $Answer\n\n";