Date Validation not working


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Date Validation not working
# 1  
Old 06-02-2014
Date Validation not working

Hi Experts,

I have a date validation script in that i will validate the date for a given format and search in the logs for that date. The script logic is very simple like below.


Code:
Validate_Date()
{
is_valid=1
while [ $is_valid -ne 0 ]
do
 date_format=$(date "+$1")
 echo -e "Please enter the $2 date like - \033[0;32m$date_format\033[0;37m"
 read MY_DATE
 tmp=$(date "+$1" -d "$MY_DATE" 2>&1)
 if [ "${tmp:6:7}" == "invalid" ]; then
  is_valid=1
  echo -e "Invalid date $tmp"
 else
  MY_DATE=$tmp
  is_valid=0
 fi
done
}


This is working fine for almost all date format expect the below one

Code:
%d/%m/%Y

Whenever I pass some inputs which is valid its throwing error

Code:
$Validate_Date "%d/%m/%Y"
Please enter the  date like - 02/06/2014
25/05/2014
Invalid date date: invalid date `25/05/2014'
Please enter the  date like - 02/06/2014

The reason is server is always taking MMDDYYYY indeed of DDMMYYYY.

Code:
$date -d "30/06/2014" "+%d/%m/%Y"
date: invalid date `30/06/2014'
$date -d "25/05/2014" "+%d/%m/%Y"
date: invalid date `25/05/2014'


How to resolve this.

Thanks
Senthil
# 2  
Old 06-02-2014
Quote:
Originally Posted by senthil.ak
The reason is server is always taking MMDDYYYY indeed of DDMMYYYY.

Code:
$date -d "30/06/2014" "+%d/%m/%Y"
date: invalid date `30/06/2014'
$date -d "25/05/2014" "+%d/%m/%Y"
date: invalid date `25/05/2014'


How to resolve this.

Thanks
Senthil
From the manual:
Quote:
-d, --date=STRING
display time described by STRING, not ‘now’

DATE STRING
The --date=STRING is a mostly free format human readable date string
such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29 16:21:42" or
even "next Thursday". A date string may contain items indicating cal-
endar date, time of day, time zone, day of week, relative time, rela-
tive date, and numbers. An empty string indicates the beginning of the
day. The date string format is more complex than is easily documented
here but is fully described in the info documentation.

It recognizes:

Code:
date --date="1 days ago"
date --date="yesterday"
date --date='10 month ago'
date --date='2 hour ago'
date --date='Second Friday'

It will read this string always 30/06/2014 as MM/DD/YYYY regardless since it can not know what you mean otherwise.

And it is independent of the wanted output format "+%d/%m/%Y"
To fix it, you do not give it nn/nn/nnnn unless you mean MM/DD/YYYY

As a note, it doesn't know "new year" neither. Darn it! Smilie
Code:
date -d "new year"
date: invalid date `new year'

This User Gave Thanks to Aia For This Post:
# 3  
Old 06-02-2014
So Aia, How to overcome the situation, I need to validate the date for the given format and if the format is not correct then i need to change or ask for the again. Do you have any logic please elaborate, thanks
# 4  
Old 06-02-2014
Quote:
Originally Posted by senthil.ak
So Aia, How to overcome the situation, I need to validate the date for the given format and if the format is not correct then i need to change or ask for the again. Do you have any logic please elaborate, thanks
As I mentioned before:
Quote:
To fix it, you do not give it nn/nn/nnnn unless you mean MM/DD/YYYY
Can you validate what I mean when I say 06/05/2014? Am I saying Jun 05 2014 or am I saying May 06 2014?

I suppose you might try at guessing, but it will not be validating it.
Unless you only accept one interpretation, you can not remove the ambiguity.

Last edited by Aia; 06-02-2014 at 11:20 PM..
# 5  
Old 06-03-2014
Hi Aia, Here i know the format, that is constant its either
Code:
 %d/%m/%Y or %Y/%m/%d

or any other format, we need to validate user input for the format. User inputs may vary and until we wont exit the loop unless user enters the correct format.
# 6  
Old 06-03-2014
Quote:
Originally Posted by senthil.ak
Hi Aia, Here i know the format, that is constant its either
Code:
 %d/%m/%Y or %Y/%m/%d

or any other format, we need to validate user input for the format. User inputs may vary and until we wont exit the loop unless user enters the correct format.
I suppose if you want to play guessing you can always try to convert anything that it is not %Y/%m/%d to it.

Some possibility in the shell
Code:
big_endian () {

data=${1}
segment1=${data%%/*}
segment3=${data##*/}
tmp=${data%/*}
segment2=${tmp#*/}

[[ "${segment1}" == ?? ]] && data="${segment3}/${segment2}/${segment1}"

echo ${data}
}

Maybe shell and external tr call
Code:
big_endian () {

data=${1}

segments=($(echo ${data} | tr '/' ' '))
[[ "${segments[0]}" == ?? ]] &&  data="${segments[2]}/${segments[1]}/${segments[0]}"

echo ${data} 
}

How about plain awk?
Code:
$date_string=$(echo $date_string | awk '{if ($1 ~ /^[0-9][0-9]$/){ print $3,$2,$1} else {print}}' FS='/' OFS='/')

As I said it is just playing a guessing game.

Last edited by Aia; 06-03-2014 at 05:04 AM..
# 7  
Old 06-03-2014
I Did some changes on the method and finally arrived to the below solution which works fine for me.
Code:
Validate_Date()
{
is_valid=1
while [ $is_valid -ne 0 ]
do
 date_format=$(date "+$1")
  echo -e "Please enter the $2 date like - \033[0;32m$date_format\033[0;37m"
 read MY_DATE
 #date "+$1" -d "$MY_DATE" 2>1 > /dev/null
 tmp=$(date "+$1" -d "$MY_DATE" 2>&1)
 if [ "${tmp:6:7}" == "invalid" ]; then
  is_valid=1
 else
  #MY_DATE=$tmp
  D=$(echo $1 | awk '{print substr($0,2,1)}')
  if [ "$D" == "d" -o "$D" == "D" ] ; then
   S=$(echo $tmp | awk '{print substr($0,3,1)}')
   MY_DATE=$(echo $tmp | awk -v "SEP=$S" 'BEGIN {FS = SEP;} {print $2SEP$1SEP$3}')
  else
   MY_DATE=$tmp
  fi
  is_valid=0
  MY_MONTH=$(date "+%b" -d "$tmp")
  MY_DAY=$(date "+%d" -d "$tmp")
 fi
done
}

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. What is on Your Mind?

Date validation

Hi folks, I new to shell script . I want to know how to validate a String as valid date example: 20150712 ---> valid date 20160524-->valid 201605T12-->invalid date 12341234--->invalid date we need to valid string( yyyymmdd) to date in SunOS 5.10 please give some idea to validate... (9 Replies)
Discussion started by: srinadhreddy27
9 Replies

2. Programming

Date validation in mysql

Hi All, We need to create the custom function to pass the parameter is date.if it is valid return 1 else 0 return should be 1 select is_date('2012-09-17'); return should be 0 select is_date('2012-79-17'); Thanks (2 Replies)
Discussion started by: bmk
2 Replies

3. Shell Programming and Scripting

Date validation

File contains below data,how to validate the date using awk command or any command. date formate is fixed as "YYYYMMDD" test1|20120405 test2|20121405 output should be: test1|20120405 Thanks (2 Replies)
Discussion started by: bmk
2 Replies

4. UNIX for Dummies Questions & Answers

Unix date validation

Dears, I am working on a batch that processes file with name containing date prefix eg., 20101222_file.dat. The logic is to process files in order. Eg., 20101225 must be processed only after 20101222. Ok first glance it looked simple, it use a variable to check this date value as number and... (2 Replies)
Discussion started by: naraink
2 Replies

5. Shell Programming and Scripting

Date validation

Hi, I have a script which runs on specific sunday. If that script runs on the sunday i want to execute another script on following wednesday. I have a log for that server. My wednesday scripts needs to check the sunday run log timestamp and if it matches it should run. Please help. Thanks,... (1 Reply)
Discussion started by: Krrishv
1 Replies

6. Shell Programming and Scripting

Date Validation in unix

I have a script which is take date as parameter sh abc.sh <2010-02-01> #!/sh/bin my_date=$1 #Here i want to two diffrent dates ## 3 Days before ##date14query=$mydate - 4 (it will be 2010-01-28) ##date24query=$mydate +4 (it will be 2010-01-05) #Please Help (3 Replies)
Discussion started by: pritish.sas
3 Replies

7. Shell Programming and Scripting

Date Validation

the user have to input the date format in mmddmmhhyyyy (month,date,minutes,hour,year) i want a shell script to check whether the user has properly input in the above said manner. kindly advice (2 Replies)
Discussion started by: vkca
2 Replies

8. Shell Programming and Scripting

Basic date validation help

Hi, I've made a very basic date validation script, but the syntax of the until condtion is wrong, could someone have a quick look and correct it please?:) Thanks for any help. echo -n "Please enter your date of birth (dd-mm-yyyy): " read dob day=${dob:0:2} month=${dob:3:2} year=${dob:6:4}... (1 Reply)
Discussion started by: mustaine85
1 Replies

9. Shell Programming and Scripting

Date Validation:

Hi I have below file with 3 rd column as date ....i want to make 3 column to mm/dd/yyyy . in the below file 2 row date is like 1/23/1994 so i want to append '0' to month i,e. 01/23/1994 and in the 3 row date is like 6/4/1994 ---so i want this to 06/04/1994 Source:... (1 Reply)
Discussion started by: satyam_sat
1 Replies

10. Shell Programming and Scripting

Date Validation again

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... (4 Replies)
Discussion started by: dave_nithis
4 Replies
Login or Register to Ask a Question