Results 1 to 2 of 2

Thread: unix script that handles dates

  1. #1
    Join Date
    Mar 2011
    Beans
    20

    unix script that handles dates

    I have a comma delimited text file that has a date field that looks like:
    12/1/2011 04:00:00 PM

    I only want to include records that are 10-40 days from now. So in this example, I'd want to include dates between 12/10/2011 and 1/10/2012. Is there a linux command or a scripting language that can easily handle this type of situation? Thanks.

  2. #2
    Join Date
    Apr 2012
    Beans
    5,385

    Re: unix script that handles dates

    The GNU 'date' function can do some pretty clever things - you could certainly pull in the date strings and convert them to some unambiguous format (e.g. +%s seconds since 1970-01-01 00:00:00 UTC) and then do a direct integer comparison

    Code:
    $ start=$(date --date="$date +10 days" +%s); stop=$(date --date="$date +40 days" +%s); echo $start $stop;
    1351093388 1353685388
    Code:
    $ inputdate=$(date --date="12/1/2011 04:00:00 PM" +%s)
    $ if [[ $inputdate > $start && $inputdate < $stop ]]; then echo "yes"; else echo "no"; fi
    no
    Code:
    $ inputdate=$(date --date="11/1/2012 04:00:00 PM" +%s)
    $ if [[ $inputdate > $start && $inputdate < $stop ]]; then echo "yes"; else echo "no"; fi
    yes

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •