Date operations in Unix?

Hi Friends,

I need help in below requirements,

  1. I have to get current datetime + <mins> into a variable
  2. I have to compage dates like,
    A=01-JAN-2009 10:20:10
    B=01-JAN-2009 10:30:00

C=<same format date as above>
I have to find whether,

  1. C is less than A OR,
  2. C is greater than B OR,
  3. C is in between A and B.

Please help me in this.

Thanks,
Raja.

TRy Perderabo's datecalc script:
http://www.unix.com/answers-frequently-asked-questions/13785-yesterdays-date-date-arithmetic.html

Convert A to epoch seconds (%s), then B to epoch seconds (%s), C to epoch seconds.
You now have three numbers to compare to answer your questions.

Try this: In my example below, it is assuming B is always greater than A. This could be coded to where it doesn't matter as well.

#!/bin/ksh

DATE_A=$(date +%Y%m%d%H%M%S -d "$1")
DATE_B=$(date +%Y%m%d%H%M%S -d "$2")

DATE_C=$(date +%Y%m%d%H%M%S)

if [ "${DATE_C}" -gt "${DATE_B}" ]; then
   echo "C, ${DATE_C},  is greater than B, ${DATE_B}"
elif [ "${DATE_C}" -gt "${DATE_A}" ]; then
   echo "DATE_C, ${DATE_C},  is between A, ${DATE_A}, and B, ${DATE_B}"
else
   echo "C, ${DATE_C},  is less than A, ${DATE_A}"
fi

tests

$./chk_date.ksh "01-JAN-2009 10:20:10" "12-DEC-2009 10:30:00"
C, 20091125051733,  is between A, 20090101102010, and B, 20091212103000

$ ./chk_date.ksh "01-DEC-2009 10:20:10" "12-DEC-2009 10:30:00"
C, 20091125051752,  is less than A, 20091201102010

$ ./chk_date.ksh "01-JAN-2009 10:20:10" "01-JAN-2009 10:30:00"
C, 20091125051824,  is greater than B, 20090101103000

This is working fine..
Thank you friend!

For date B, I need to add some mins with current date. How could i get it?

---------- Post updated at 10:36 PM ---------- Previous update was at 10:34 PM ----------

I have seen epoch seconds method in net, It is using perl command.
I do not have perl in my system. Thanks for your quick response friend :slight_smile:

Can you explain what you mean by needing to add some minutes with current date?

I have to do comparison between two date values. One is current date time (min) and another date (max) will be calculated by adding some input minutes with current date time.

Pls help to get the above requirement.
Ex:
Min_Date=Current Datetime (Format: DD-MON-YYYY HH24:MI:SS)
Max_Date=Current Datetime + X mins (Format: DD-MON-YYYY HH24:MI:SS)

Check out the link provided by Jim McNamara above. It contains links to other solutions as well. If none of these work for you, let me know and I can dig up a function I wrote to do this. That was several jobs ago though and I'm not sure where I have it.