Date Validation again

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 23
Reputation: dave_nithis is an unknown quantity at this point 
Solved Threads: 0
dave_nithis dave_nithis is offline Offline
Newbie Poster

Date Validation again

 
0
  #1
Jan 4th, 2008
I want a sample Date validation script using if loops.
The script should first compare "year".If the year is lesser than the current year,It should go for "month" checking.
I have the script that splits the date into year,month and date.I want only the checking part.
My if loop checking is not work properly.
Can you show the right way of checking?

Regards
Dave
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,397
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 255
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Date Validation again

 
0
  #2
Jan 4th, 2008
Show what you have, and we will help you correct it.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 23
Reputation: dave_nithis is an unknown quantity at this point 
Solved Threads: 0
dave_nithis dave_nithis is offline Offline
Newbie Poster

Re: Date Validation again

 
0
  #3
Jan 7th, 2008
This is the splitting part am having:

C_Date=`date -u +%m/%d/%Y`

year="$(echo $Date | cut -d/ -f3)"
month="$(echo $Date | cut -d/ -f1)"
day="$(echo $Date | cut -d/ -f2)"

cyear="$(echo $C_Date | cut -d/ -f3)"
cmonth="$(echo $C_Date | cut -d/ -f1)" cday="$(echo $C_Date | cut -d/ -f2)"
C_Date is current date i.e Today's Date.

#Validation
if [ $year -gt $cyear ]; then
echo "year out of range"
fi
Similarly am checking for month and day.Is there any other simple way to do the comparison using case Statement?


Regards
Dave.
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 178
Reputation: jim mcnamara is on a distinguished road 
Solved Threads: 10
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: Date Validation again

 
0
  #4
Jan 10th, 2008
date -u causes UTC date/time to be displayed - FYI.

If you format date like this: [code]
today=$(date +%Y%m%d)
you get a value like 20080111, this is a number - an integer.

When you split the input reformat it:
Shell Scripting Syntax (Toggle Plain Text)
  1. newdate="$cyear$cmonth$cday"
then compare integers:
Shell Scripting Syntax (Toggle Plain Text)
  1. if [[ $today -ne $newdate ]]
  2. then
  3. echo "bad date"
  4. fi
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC