To compare two dates the most common procedure is to convert both to some integer value and then compare these. Most commonly in UNIX and UNIX-like systems the conversion is done to seconds passed since Jan 1st, 1970, 0:00 am (the so-called "epoch" or "unix time"). For instance, as i am writing this this time value is "1472042505". It makes it easy to calculate a duration by simply subtracting one date in this form from the other.
There are many ways of converting time values to unix time format, here is one in Korn Shell 93 using the printf command:
In addition to what Bakunin had mentioned already, you could try following too in BASH, by converting time to epoch time and then calculate.
Thanks,
R. Singh
Good tip also, if the date is in format specified : YYYY MM DD ...<everything else>
A simple numeric comparison on integer value would suffice -gt, -lt ,-eq , if you do not wish to use epoch or date utility.
Of course, one would need to strip out the input not required (leave only numbers), which should not be too much of a problem.
To the shell, < is the input redirection operator. To use it in comparisons, you need to escape it:
Or even
Hello RudiC
Thanks for your response. But your second suggestion will not work correctly. It will give the output yes in any case irrespective of the condition is true or false.
Thanks
This User Gave Thanks to Ramneekgupta91 For This Post:
With the format of the strings you are comparing, a string comparison is sufficient, but that type of comparison is not supported by the standard test expressionand [ expression ] utilities. If you're using a recent bash or a 1993 or later version of ksh as your shell, you could use the conditional expression (as suggested by RavinderSingh13 that has been added to the syntax of their command languages:
or more simply:
This should work fine up to the point that you want to compare a date in the year 9999 to a date in the year 10000.
Note that the command Ravinder suggested:
is performing a string comparison (even though VAL1 an VAL2 have been set to numeric strings. This will work as long as both numeric strings contain the same number of digits, but is dangerous in the general case. When comparing numeric strings it is safer to use the numeric comparison operators (-lt, -le, -eq, -ge, -gt, and -ne) instead of the string comparison operators (<, <=, ==, >=, >, and !=) available in the [[ expression ]] syntax.
This User Gave Thanks to Don Cragun For This Post:
I need to be able to compare dates in the format of Jan 10, 2012 and Jan 10 2012. (Notice one has a comma).
Then I need to find the date that is 7 days before those dates if they are equal.
How can I do this in Bash.
Thank ahead (4 Replies)
Hi All,
I am entering StartDate and EndDate as parameters to script. Want to have an check saying, "If StartDate is greater than EndDate then don't execute the script".
Pseudo Code:
if
then
Execute script
else
exit 0
fi
Can you please help me on the same?
Thanks and... (4 Replies)
I want to compare a list of dates in a file with today's date & list only dates that are less than only 60 days old . please help . the date in the file are in format
11-FEB-2009
02-FEB-2009
26-JAN-2009
24-JAN-2009
13-JAN-2009
16-DEC-2008
10-DEC-2008
01-DEC-2008
25-NOV-2008
19-NOV-2008... (3 Replies)
Need to find all records where date in one filed is greater than date in other.
Input:
ABC 2 Filed3 CDG * X 20080903 20081031 180.00
ABD 2 Filed3 CDG * X 20081101 20081031 190.00
ABE 2 Filed3 CDG * X 20090903 20081031 120.00
ABC 2 Filed3 CDG * X 20080903 20081015 130.00
Output:
... (2 Replies)
I have filenames
filenameA_fg_MMDDYY.tar.gz
filenameASPQ_fg_MMDDYY.tar.gz
filenameAFTOPHYYINGH_fg_MMDDYY.tar.gz
filenameAGHYSW_fg_MMDDYY.tar.gz
My requiremnt needs to extract date which is in MMDDYYand change it into YYYYMMDD format.
I have done the following:
filedate=`echo... (5 Replies)
Hello friends,
I am looking for a script or method that can display all the dates between any 2 given dates.
Input:
Date 1
290109
Date 2
010209
Output:
300109
310109
Please help me. Thanks. :):confused: (2 Replies)
Hi all,
How to check whether the given the two dates is minimal.
example:
Date 1 : 23-03-2008 with timestamp
Date 2: 20-03-2008 With tmestamp
I want to compare the twodates and which it gives the minimum date
i wnat to get the output like this below
output: the Date2 is... (1 Reply)
Hi all,
I've written a script which gives the below information...
End Date&Time: 2008-10-21 10.54.37
Now i want to calculate this time with the current time.. and if its more than 48 hours past with the current time it should echo "48 Hours back"
Please help me..
thanks in... (4 Replies)
hi all :)
how can in compare yyyy/dd/mm with yyyy/dd/mm in perl i want the result like grater than or less than the given date...
thanks in advance (3 Replies)
I have a log file with date format like
10-Oct-02 13:20:29 .....
at the beginning of each line in the log file, and I need to grep data from this file to list the lines with date no longer than one days.
I tried to use awk to do this but it looks very complicated to do it.
Is there... (6 Replies)