mt2k has asked for the wisdom of the Perl Monks concerning the following question:
Mmm... Gotta weird question here:
If I have the date May 25, 2002 (05/25/2002), how can I get the date of the month for that date??
So if that is a Monday, I want to have Monday in a variable.
And no doubt somebody is going to look that date up and say that it is not a Monday... =8-)
$$mt2k{'mood'} = "Happy :-)";
$perlmonks{'mt2k'} = $mt2k;
(Ovid) Re: Getting Day Of Month
by Ovid (Cardinal) on Jan 03, 2001 at 00:56 UTC
|
One way is to install Date::Manip. Then, extract out the month, day, and year with a regex. Create an array with the days of the week.
use Date::Manip;
my @dayOfWeek = qw(Sunday Monday Tuesday Wednesday Thursday Friday Sat
+urday);
my ( $month, $day, $year ) = ( 5, 25, 2002 );
my $day = &Date_DayOfWeek( $month, $day, $year );
print $dayOfWeek[ $day ];
And no doubt somebody is going to look that date up and say that it is not a Monday... =8-)
According to Date::Manip, it's a Saturday :)
Cheers,
Ovid
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats. | [reply] [d/l] |
|
#!/usr/bin/perl -w
use strict;
use Date::Calc qw/Day_of_Week/;
my @days = qw/Sunday Monday Tuesday Wednesday
Thursday Friday Saturday/;
my $dow = $days[ Day_of_Week(2002, 5, 25) ];
print "$dow\n";
__END__
Several have mentioned simple ways to do it yourself, but if you
need more date manipulation than your post suggests (things like
differences between dates, business days, etc), I recommend checking
out one of the modules -- it isn't hard to work with dates, but it
isn't hard to work with dates incorrectly either :-)
| [reply] [d/l] |
Re: Getting Day Of Month
by tune (Curate) on Jan 03, 2001 at 01:04 UTC
|
i am sure there is a smart module out there to solve this,
but i would do the following:
1.) use Time::Local - with the help of this module
you can convert the date (may 25 2002) to Unix time.
2.) and then localtime($your_unix_time) will give you the weekday (check localtime manual) --
tune | [reply] [d/l] [select] |
Re: Getting Day Of Month
by cat2014 (Monk) on Jan 03, 2001 at 00:59 UTC
|
use Date::Manip
$day=&Date_DayOfWeek($m,$d,$y);
If you have any funny date/time stuff that you want to do, a good first step is
always "perldoc Date::Manip" because most likely it can do
exactly what you want.
| [reply] [d/l] |
Re: Getting Day Of Month
by lhoward (Vicar) on Jan 03, 2001 at 01:02 UTC
|
use Time::ParseDate;
use Time::CTime;
my $day_of_week=strftime("%A",localtime(parsedate("May 25, 2002")));
(by the way, "May 25, 20002" is a Saturday).... | [reply] [d/l] |
Re: Getting Day Of Month
by I0 (Priest) on Jan 03, 2001 at 01:03 UTC
|
use Time::Local;
$day = unpack"a3",localtime timelocal 0,0,12,25,5-1,2002-1900;
| [reply] [d/l] |
Re: Getting Day Of Month
by ichimunki (Priest) on Jan 03, 2001 at 00:44 UTC
|
Convert 05/25/2002 to epoch date, use localtime. | [reply] |
|
Or use Date::Format; and say time2str("%A",$time);
You did want Saturday, right?
Update: I got ahead of myself. time2str
uses a epoch time which you could get from timelocal. Turns
out, I should have used strftime("%A", ['','','',25,05,2002]);
ALL HAIL BRAK!!!
| [reply] [d/l] [select] |
|
Um, what do I do for that localtime() part??
Or Psycho, what does $time hold?
Could you give me the code, I'm slow.
Update: Hey, isn't the Date module standard??
I have to install the stupid thing... Unbelievable!
| [reply] |
|
While I strongly suggest taking cat2014's advice... The localtime function converts an Epoch date into a list. If you look at the documentation, it has (IIRC) nine elements, of which one is the number of the day of the week ($wday). Which you can then use to extract the day of the week from your own @days = ( Sun, Mon, Tue, Wed, Thu, Fri, Sat) list, using $days[$wday]. Update: Getting your input date into Epoch time without a module like Date::Manip, however, is tricky as all heck and not recommended.
| [reply] [d/l] |
Re: Getting Day Of Month
by damian1301 (Curate) on Jan 03, 2001 at 02:11 UTC
|
Well I think Date::Calc from CPAN would do it. Also, if you want to get a neat module to find out what day Christmas is going to be, you should check out Date::Christmas. But you should also search for 'Date' at http://search.cpan.org or here.
Wanna be perl hacker. Dave AKA damian | [reply] |
|
|