Forum Moderators: coopster & phranque

Message Too Old, No Replies

Get Day of Week from MM/DD/YY date format.

         

JohnKelly

4:10 pm on May 6, 2005 (gmt 0)

10+ Year Member



How does one obtain the day of the week in Perl, with the date in the format MM/DD/YY?

The dates are already in that format in a file... must I convert to epoch seconds first or is there a shortcut?

TIA

wruppert

10:32 pm on May 6, 2005 (gmt 0)

10+ Year Member



I like Date::Parse and Date::Format. Others like Class::Date.

use Date::Parse;
use Date::Format;

my $date = str2time "05/06/2005";
print "mm/dd/yy: ", time2str ("%D", $date), "\n";
print "Day of week: ", time2str ("%A", $date), "\n";

FlyingWulf

6:17 am on May 28, 2005 (gmt 0)

10+ Year Member



as there are several different ways of doing something in perl something that I use is

[code]

use POSIX qw(strftime);
$date = strftime('%m/%d/%Y', localtime()),

[\code]

;-)

Have a wodnerful day

lexipixel

10:43 am on May 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



...some dating advice: be careful when working with date related modules.

Check to see if the year '05' is the same as '2005' (or it could be "the year 5 A.D." or 1905)... Some times you need to add 1900.. others you don't.

Also be sure if the month is zero or one based.

I have ripped out many hairs working with dates.

The safest thing is to ALWAYS convert to 4-digit year and establish 1 or 0 base for months at top of code, (ie- after any input is parsed, normalize your dates to YYYYMMDD format).

When all is said and done, I check the dates January 1st, February 28th and/or 29th and December 31st for a few known years just to make sure things work as expected. Ugly problems will reveal themselves on these dates if there are problems.

It also helps if your data is in a known range of years. Some modules, languages or operating systems can only handle certain date ranges, usually 1980-2099 are safe.