How to compare two dates in a specific format ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to compare two dates in a specific format ?
# 1  
Old 08-24-2016
How to compare two dates in a specific format ?

Hello,

I am not able to find how to compare two dates in '%Y-%m-%d %H:%M:%S,%3N' format

i have to two dates as below

Code:
date1="2016-08-24 09:47:40,444"
date2="2016-08-24 10:45:40,567"

how to compare these two dates ?

I have tried below without success

if [ "$date1" < "$date2" ]; then echo 'yes'; fi

if [ "$date1" -le "$date2" ]; then echo 'yes'; fi



Please suggest.

Thanks

Last edited by RudiC; 08-24-2016 at 09:33 AM..
# 2  
Old 08-24-2016
To the shell, < is the input redirection operator. To use it in comparisons, you need to escape it:
Code:
 if [ "$date1" \< "$date2" ]; then echo 'yes'; fi
yes

Or even
Code:
if [ "$date1 < $date2" ]; then echo 'yes'; fi
yes

This User Gave Thanks to RudiC For This Post:
# 3  
Old 08-24-2016
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:

Code:
# printf "%(%s)T\n" now
1472042505

I hope this helps.

bakunin
# 4  
Old 08-24-2016
Hello Ramneekgupta91,

In addition to what Bakunin had mentioned already, you could try following too in BASH, by converting time to epoch time and then calculate.
Code:
cat script.ksh
date1="2016-08-24 09:47:40,444"
date2="2016-08-24 10:45:40,567"
VAL1=$(date +%s -d"$date1")
VAL2=$(date +%s -d"$date2")

if [[ $VAL1 < $VAL2 ]]
then
        echo "Yes"
else
        echo "No"
fi

Thanks,
R. Singh
# 5  
Old 08-24-2016
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.

Best regards
Peasant.
# 6  
Old 08-24-2016
Quote:
Originally Posted by RudiC
To the shell, < is the input redirection operator. To use it in comparisons, you need to escape it:
Code:
 if [ "$date1" \< "$date2" ]; then echo 'yes'; fi
yes

Or even
Code:
if [ "$date1 < $date2" ]; then echo 'yes'; fi
yes


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:
# 7  
Old 08-24-2016
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:
Code:
if [[ $date1 < $date2 ]]; then echo yes; fi

or more simply:
Code:
[[ $date1 < $date2 ]] && echo yes

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:
Code:
if [[ $VAL1 < $VAL2 ]]
then
        echo "Yes"
else
        echo "No"
fi

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:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How do i compare two dates with format Jan 01, 2012 and Jan 00 2012

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)
Discussion started by: ojthejuice
4 Replies

2. Shell Programming and Scripting

Compare Dates.

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)
Discussion started by: Nagaraja Akkiva
4 Replies

3. Shell Programming and Scripting

compare dates

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)
Discussion started by: skamal4u
3 Replies

4. Shell Programming and Scripting

Compare dates

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)
Discussion started by: necroman08
2 Replies

5. Shell Programming and Scripting

change date format and then compare dates

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)
Discussion started by: RubinPat
5 Replies

6. Shell Programming and Scripting

Need script to generate all the dates in DDMMYY format between 2 dates

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)
Discussion started by: frozensmilz
2 Replies

7. Shell Programming and Scripting

compare between the two dates

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)
Discussion started by: balaji23_d
1 Replies

8. Shell Programming and Scripting

How to compare the dates..

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)
Discussion started by: suri.tyson
4 Replies

9. Shell Programming and Scripting

compare dates...

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)
Discussion started by: i_priyank
3 Replies

10. Shell Programming and Scripting

compare two dates

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)
Discussion started by: wchen
6 Replies
Login or Register to Ask a Question