Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Getting Day Of Month

by mt2k (Hermit)
on Jan 03, 2001 at 00:40 UTC ( [id://49366]=perlquestion: print w/replies, xml ) Need Help??

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;

Replies are listed 'Best First'.
(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.

      An alternative to Date::Manip is the Date::Calc module which also provides hordes of date utility functions (and it is a C lib so it is fairly quick).

      #!/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 :-)

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

Re: Getting Day Of Month
by cat2014 (Monk) on Jan 03, 2001 at 00:59 UTC
    My favorite way to do that would be with Date::Manip -

    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.

Re: Getting Day Of Month
by lhoward (Vicar) on Jan 03, 2001 at 01:02 UTC
    Or you could use Time::ParseDate and Time::CTime (both come together in the time-modules bundle).
    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)....
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;
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.
      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!!!

      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!

        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.
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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://49366]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-25 22:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found