Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Comparing Dates

by joshua (Pilgrim)
on Feb 21, 2002 at 00:50 UTC ( [id://146673]=CUFP: print w/replies, xml ) Need Help??

You can check if the current date and time came after a specified date and time by using this format.
YYYYMMDDHHMMSS
Right now, it would be 20020220194516.
my $comparetodate = 20020220194516; # This would probably be taken fro +m a database or something. my ($sec, $min, $hour, $mday, $mon, $year) = localtime; my $date = sprintf "%04d%02d%02d%02d%02d%02d",$year+1900,$mon+1,$mday, +$hour,$min,$sec; print "Content-type: text/html\n\n"; if ($date > $comparetodate) { print "Right now is after the archived date."; } else { print "Right now is before the archived date."; }
The hour must be in 24-hour format. You don't have to use the hour, minute, and second if you just want to compare the date and not the time.

-joshua

Replies are listed 'Best First'.
Re: Comparing Dates
by grep (Monsignor) on Feb 21, 2002 at 02:25 UTC
    This functionalty is taken care of in the Date::Calc module. Date::Calc can do all of your date caclulations without the running your dates through sprintf or worrying about 24 hour formats or worrying about 2/4 digit years.

    Else if you really want down to the second comparisions then just use epoch seconds. Epoch seconds are the native format for localtime.

    I would highly recommend reading some of these nodes before you post again:
  • Before you post...
  • turnstep's home node
  • How to ReadTheFineManual

  • Understanding these posts will make your stay at the Monastery more enjoyable.

    Understanding why you should not reinvent good wheels will make your career more enjoyable.

    grep
    grep> rm -f /bin/laden
Re: Comparing Dates
by lachoy (Parson) on Feb 21, 2002 at 03:22 UTC

    What's funny is that Calculating 1-day-before, with tons of great suggestions -- especially that one mentioning Class::Date... :-) -- just appeared a couple days ago...

    Chris
    M-x auto-bs-mode

      I think I like Date::Handler slightly better than Class::Date.

      metadoktor

      "The doktor is in."

        This looks interesting (pure-perl and all), although the fact that it claims it's incomplete at this time would lead me to set it aside and check it later...

        <rant mode="on">

        As an aside: why do people ignore stated and historical Perl conventions and create methods with StudlyCaps rather than underscored_names? The only place this should be accepted is if a module is echoing an API found from another language that does use StudlyCaps as a convention, like libservlet does with the Java Servlet API.

        I understand that people moving from another language where StudlyCaps are used might have a little dissonance with underscores, but a message for these folks: it goes away! I code lots of Java and lots of Perl every day and have no issues with it -- Java has StudlyCaps, Perl has underscored_names. Perl has sigils, Java doesn't. ... It's not that hard! It's all just syntax!

        </rant>

        Chris
        M-x auto-bs-mode

Re: Comparing Dates
by mortis (Pilgrim) on Feb 21, 2002 at 05:03 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2024-04-19 20:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found