#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Location
    Phoenix AZ
    Posts
    30
    Rep Power
    19

    KSH - Test if Variable is a Date


    I have tried looking this up, but all I get is how to display a date. I have a parameter that is being passed that I need to know if it is a valid date. What I do right now is split it up into its components - year, month, day - then do the following:
    • Test if year is between 2000 and the current year
    • Test if month is between 1 and 12
    • test if day is between 1 and the maximum number allowed for the month in question


    There must be a better way to test if the date being passed is valid. Thanks for your help.
    Programmer at large
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2007
    Posts
    168
    Rep Power
    7
    Use GNU date
    The GNU date command accept a date as input
    $ date -d '20130228'
    Thu Feb 28 00:00:00 SAST 2013
    $ echo $?
    0
    $ date -d '20130229'
    date: invalid date `20130229'
    $ echo $?
    1
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Location
    Phoenix AZ
    Posts
    30
    Rep Power
    19

    Didn't work


    I know it should have worked, but I got the following instead (default is in /usr/bin):


    date: illegal option -- d
    usage: date [-u] mmddHHMM[[cc]yy][.SS]
    date [-u] [+format]
    date -a [-]sss[.fff]

    Tried the one we commonly use in /usr/app/bin and had to make one alteration: "date --d='20130228'". However... (welcome to my world)...

    If I endter "date --d='20131431'" I get this:

    Mon Mar 3 00:00:00 MST 2014

    It's like it considers that you might have meant to do that. I can run it so that it checks to make sure that the date is no greater than one month from today (--d='+31') but I was hoping for a function or something that already existed. Apparently we don't have the GNU date at all.



    Thanks for trying though. I thought for sure that would work. As I said, welcome to my world.

    Originally Posted by pdreyer
    Use GNU date
    The GNU date command accept a date as input
    $ date -d '20130228'
    Thu Feb 28 00:00:00 SAST 2013
    $ echo $?
    0
    $ date -d '20130229'
    date: invalid date `20130229'
    $ echo $?
    1
    Programmer at large
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,464
    Rep Power
    1751
    The GNU date executable is (often) not a part of a standard Unix install (hence the name, really!) so you will have to find a version compatible with your *nix system, downlaod and install it.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo