Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Compare two dates

by Ratazong (Monsignor)
on Jul 23, 2019 at 05:55 UTC ( [id://11103191]=note: print w/replies, xml ) Need Help??


in reply to Compare two dates

As others wrote, with the date-format chosen by you, you just have to apply normal string compare operators.

However in case you have other date formats (e.g. the format DD.MM.YYYY, which is popular in Germany), this approach won't work.

I made good experience in converting the date into a number (e.g. DD + 100*MM + 10000*(YYYY-2000) ), and then comparing them.

HTH, Rata

Replies are listed 'Best First'.
Re^2: Compare two dates
by Marshall (Canon) on Jul 23, 2019 at 06:31 UTC
    Yes, in the USA: MM/DD/YYYY is also common. However just like DD.MM.YYYY, there may not be leading zero'es for the days or months.

    I prefer one of the 12 formats shown in my previous post so that an ASCII sort order will work. Converting a date to a numeric value for comparisons will work and I don't have any big quibble with that. But I would run a regex to convert: DD.MM.YYYY to YYYY-MM-DD for storage and processing purposes. Wenn der Benutzer ein Deutscher ist, würde ich die Ziffernreihenfolge für Anzeigezwecke ändern. Or maybe not? My German is not that great!

    Update:
    I use YYYY-MM-DD as expressed in GMT/UTC time zone for all of my logging. If some conversion to local time formats for a user display is necessary, I do that. However almost all of my work is in GMT/UTC. Yes there is a small technical difference between these two definitions of time. For my work, this difference just doesn't matter. Some DB's use a numeric value for the date and some use a string. Any DB that uses a numeric value for a date, has a function to generate that numeric value from YYYY-MM-DD.

    I use the terms GMT and UTC interchangeably. See Coordinated Universal Time. This wiki article notwithstanding, I recollect that there is some extremely small difference having to do with small fractions of seconds related to this "leap second" stuff. Nobody here has to worry about UTC vs GMT => for all practical purposes, they are the same.

      Comparing strings usually takes longer than comparing numbers. Keep that in mind.
        Maybe, but it's two strings vs. two to six numbers (years, months, days) …

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-23 08:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found