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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do i compare two dates with format Jan 01, 2012 and Jan 00 2012
# 1  
Old 05-10-2012
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
# 2  
Old 05-10-2012
What's your system? If you have Linux, you'll have GNU date, which will make this pretty easy:

Code:
$ date -d 'Jan 10, 2012' '+%s'
1326175200

$ date -d 'Jan 10 2012' '+%s'
1326175200

$

That gives you the date in seconds, and finding how different they are is basic math from there.
# 3  
Old 05-10-2012
Okay thanks,

But what's wrong with this. I get this error './test.sh: line 14: test: -le: unary operator expected'

Code:
#!/bin/bash

SEC_IN_DAY=86400
EXP_DATE=`date -d 'May 17, 2012' '+%s'`
echo "Expiration date is set to May 17, 2012 and the seconds are $EXP_DATE"
SEVEN_DAYS=$(( $SEC_IN_DAY * 7))
TODAY_IN_SEC=`date '+%s'`
echo "Today in seconds is $TODAY_IN_SEC \'s"
SEVEN_DAYS_FROM_NOW=$(( $TODAY_IN_SEC + $SEVEN_DAYS))
echo "Seven days from now is $SEVEN_DAYS_FROM_NOW in seconds"
echo "Performing Test........."

#if [ "$EXPIRE_TIME" -le "$SEVEN_DAYS_FROM_NOW" ]
if test $EXPIRE_TIME -le "$SEVEN_DAYS_FROM_NOW
        then
                echo "Password is about to expire in seven days"
                echo "Please change your password"
fi

# 4  
Old 05-10-2012
EXPIRE_TIME is not set anywhere, which causes a syntax error for arithmetic comparison. Your first 'if' would have worked with "$EXP_DATE", I think.
# 5  
Old 05-10-2012
Ohh, wow.

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Date format from Jan 01 2015 11:00:00 PM to 01/01/2015 23.00.00

I need to change Date and time stamp format from Jan 01 2015 11:00:00 PM to 01/01/2015 23.00.00 Existing Format : Mon DD YYYY hh:mi:ss AM/PM (Jan 01 2015 11:00:00 PM) Expected Format: MM/DD/YYYY hh.mi.ss 24 hours (01/01/2015 23.00.00) I need to update enitire file where... (3 Replies)
Discussion started by: esivaprasad
3 Replies

2. AIX

The result of Jan 01 (1970) by last command

Dears, My boss asked me to record the login information on AIX server, so I used "last" command to get i want. But it is so strange when i get a record the login date is Jan,01 1970 (as attached picture). does anyone know what happen? Thanks and Regards, (6 Replies)
Discussion started by: Zack.Chiang
6 Replies

3. What is on Your Mind?

Place your bits - 2012 FIFA Ballon d'Or and 2012 FIFA World Coach of the Year

I have added two new sports events. The FIFA Ballon d'Or is an association football award given annually to the player who is considered to have performed the best in the previous season. It is awarded based on votes by coaches and captains of international teams, as well as journalists from... (0 Replies)
Discussion started by: ni2
0 Replies

4. Shell Programming and Scripting

Date conversion help from dd/mm/yyyy to dd/Mon/yyyy i.e. 28/10/2012 to 28/Oct/2012

Hi I have a problem with Date format in my code. 1st I am trying to convert today's date to yesterday's using YESTERDAY3=`perl -e '@y=localtime(time()-86400); printf "%04d/%02d/%02d",$y+1900,$y+1,$y;$y;'` And once it is done I am trying to using the yesterday date in a grep command to... (3 Replies)
Discussion started by: nithinankam
3 Replies

5. Shell Programming and Scripting

perl one-liner to get yesterday's date in format dd-MMM-yy (i.e. 01-JAN-12)

I have the following perl one-liner to get yesterday's date, but I would like it in the form of dd-MMM-yy (for example: 01-JAN-12). Can someone alter the below code so I get the format I want? Also, could someone also give me a line for dd-Mmm-yy (for example 01-Jan-12)? Code: YEST=`perl -w... (3 Replies)
Discussion started by: thibodc
3 Replies

6. Shell Programming and Scripting

gawk convert 2012-Jun-13 to 2012-06-13

I have a value in a file i am processing that has a date like "2012-Jun-13" how can I convert a date like that 2012-06-13? Am I stuck building an array of three digit months and corresponding numbers and running through the logic of figuring out the number?? or can I convert this with... (1 Reply)
Discussion started by: trey85stang
1 Replies

7. Shell Programming and Scripting

Calculate the calendar date since Jan 1, 2000

Does anyone know how to calculate a calendar date since Jan 1, 2000 (this is day 1). I am using CSH with a Solaris system (no GNU products installed). Example: Input from the user (number of days): 4444 Output (dd mmm yy) 02 mar 12 ---------- Post updated at 09:40 PM ----------... (9 Replies)
Discussion started by: thibodc
9 Replies
Login or Register to Ask a Question