Compare two dates using Shell Programming


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare two dates using Shell Programming
# 1  
Old 10-26-2007
Data Compare two dates using Shell Programming

Hi all,

a=2007-05-10 (YYYY-DD-MM Format)
b=2007-06-10

These are the two given dates and I need to compare.
(First It should split the dates into YYYY,dd,mm)

The script should first compare years(2007 here).If both are same or if "a" is lesser than "b"(ie.suppose year in "a" is 2006),it should print correct.If "a"(ie. suppose year in a is 2008) is greater than b,then it should print error.Similarly for the dates(05 and 06).

No need to check for the months(10).

Regards,
Dave Nithis.
# 2  
Old 10-26-2007
this might be useful
# 3  
Old 10-26-2007
convert the dates to a long integer (ksh) and then subtract
Code:
#!/bin/ksh
 a="2007-10-27"
 b="2006-03-22"
if [[ $((  $(echo $a | tr -d '-')   -  $(echo $b | tr -d '-')  )) -ge 0 ]] ; then
   print "correct"
else
   print "failure"    
fi

# 4  
Old 10-26-2007
in bash you could do
(( ${a:0:4} <= ${b:0:4} )) && echo true || echo false
or if you want to check year AND day
(( ${a:0:4} <= ${b:0:4} && ${a:5:2} <= ${b:5:2} )) && echo true|| echo false
# 5  
Old 01-04-2008
Quote:
Originally Posted by jim mcnamara
convert the dates to a long integer (ksh) and then subtract
Code:
#!/bin/ksh
 a="2007-10-27"
 b="2006-03-22"
if [[ $((  $(echo $a | tr -d '-')   -  $(echo $b | tr -d '-')  )) -ge 0 ]] ; then
   print "correct"
else
   print "failure"    
fi

Can you explain the if part?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

compare dates using shell sript

I have 2 date feilds 2011-05-13:18:45 2011-05-13:18:30 I need to compare them and say its OK/NOK I tried this but dint work. systime=2011-05-13:18:45 shubtime=2011-05-13:18:30 if then echo" OK" else echo "NOK" fi In this its not same so the o/p should be NOK (2 Replies)
Discussion started by: LavanyaP
2 Replies

2. Shell Programming and Scripting

The Best Way to Compare Dates

Hi to all. When you have to compare a lot of dates in a SH code, there is a way to directly compare? For example, how can I check if two dates differ in less than a week? Thank's for reading. (2 Replies)
Discussion started by: daniel.gbaena
2 Replies

3. 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

4. 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

5. Shell Programming and Scripting

Compare one file with 7 files in shell programming

hi , I am new to shell. i have a requirement to compare the one file from one dir to other 7 files of other dir and to find out the duplicate files from the 7 files . the name of the all files go on changing every weekend. Thanks in advance (4 Replies)
Discussion started by: ravi214u
4 Replies

6. 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

7. Shell Programming and Scripting

How to compare the dates in shell script

Hi How to compare created or modified date of two files help needed thanks Vajiramani :) (9 Replies)
Discussion started by: vaji
9 Replies

8. Shell Programming and Scripting

compare dates

Hi Gurus I am getting the timestamp of the last generated log file its like this "Oct 31 10:26" I want to compare this timestamp with the current date in shell script. I want to compare if the (timestamp-currentime) > 10 minutes how do i do this. Thanks Ragha (2 Replies)
Discussion started by: ragha81
2 Replies

9. Programming

How to compare dates in C/C++

Hi, Is there any system defined function to compare two dates in C/C++? Thanks (1 Reply)
Discussion started by: naan
1 Replies

10. Programming

How to compare two dates

Hi I am writing a unix program. In that, i should compare two dates. I would like to know how to compare two dates in unix-whether they are same or not. pls help (5 Replies)
Discussion started by: bankpro
5 Replies
Login or Register to Ask a Question