Need to validate a date input format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to validate a date input format
# 1  
Old 12-05-2002
Need to validate a date input format

Hi all,

I have a shell script(K shell) which takes a date as input.

i want the input to be in DD-MM-YYYY format.

Can i enforce such a format of input string using just one line of code?

OR

do i need to parse the input date into different components and test them using Case statements separately?

Please. suggest an easy method of doing this.

Thanks a lot,
raju
# 2  
Old 12-05-2002
One solution is to use my datecalc script.

If you do "datecalc -j yyyy mm dd" , datecalc will try to compute the modified julian day number of the date. This will fail if the date is not valid. You can simply test the return code:
Code:
if datecalc -j $year $month $day > /dev/null 2>&1  ; then
         echo date was valid
else
         echo date was invalid
fi

Here I sent stderr to /dev/null. But datecalc will write a specific error message to stderr if it detects an invalid date. You may want to just send the error on to the user.

Or you can can look at datecalc to see how to test the validity of a date. But it takes some code to this, mostly due to the complex rules involving leap year.
# 3  
Old 02-02-2009
MySQL

Quote:
Originally Posted by Perderabo
One solution is to use my datecalc script.
Hi guys, I'm new in UNIX and need to validate a date

How can I call datecalc script. from main shell?

is the same like to call another file with function, maybe

. ./datecalc_script.ksh

I tested it and dont run


thanks for your answers


grettings
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Validate date file format using awk

Here is my sample data Test.txt column 1|columne 2|start Date|end Date test|test|03/24/2014|03/24/2014 test|test|03/24-2014|03/24/2014 test|test|03/24/2014|03/24/2014 test|test|03/24/2014|03/24-2014 test|test|03/24/2014|03/24/2014 Now in the file i am expecting the date fields should be... (4 Replies)
Discussion started by: krish2014
4 Replies

2. Shell Programming and Scripting

How to validate user's input..?

$Input_filename=$ARGV; if (!-d $Input_filename && ! -e $Input_filename) { print "USAGE: Please enter '$ABCD/def/dsed.txt' as an arguement \n"; exit; } 1. Input Is suppose to be something like "$ABCD/def/dsed.txt". if the input is wrong the script should throw an ERROR message.... (2 Replies)
Discussion started by: Rashid Khan
2 Replies

3. Shell Programming and Scripting

The scope of the shell/perl script is to read the input text file. Validate the expiry date of each

The scope of the shell/perl script is to read the input text file. Validate the expiry date of each certificate and send the mail to the user. The user takes action to add the new certificate to the storage file and user owns the responsibility to update the input text file with the new certificate... (5 Replies)
Discussion started by: casmo
5 Replies

4. Shell Programming and Scripting

How to validate input parameters?

Hi, I wonder how I can know if the input parameters to the script are numbers or text Thanks (11 Replies)
Discussion started by: Gengis-Kahn
11 Replies

5. Shell Programming and Scripting

Check input date format?

how to check input date format. for example $input_date must be in format dd.mm.gg script is execute like this: bin/script1.sh 14.12.2009 script1.sh code: #!/bin/sh input_date=$1 CMD="/app/si/test/test.sh $input_date" echo "*****" $CMD (2 Replies)
Discussion started by: waso
2 Replies

6. Shell Programming and Scripting

Validate and sort input

Hi, This will most likely be a simple answer. Currently I have a situation where my script will be sent various options: -o1 -o2 -oe3@somthing.com Now, if I want to run a certain command based on the option I am sent, I am doing the following. for o in $(echo $options) do if ... (3 Replies)
Discussion started by: stuaz
3 Replies

7. Shell Programming and Scripting

Validate date format in argument

Hi.. I'm a newbie in KSH. Need help. How can I validate input date format in argument using format mm/dd/yy. Eg. > findlog.sh 12/30/09. Any info/help is highly appreciated. (3 Replies)
Discussion started by: harry0013
3 Replies

8. Shell Programming and Scripting

validate input

the user inputs names that have to be inside square brackets I want to check if the user puts the brackets and if not ask him to re-enter the names (9 Replies)
Discussion started by: DDoS
9 Replies

9. Shell Programming and Scripting

Validate long date format in If statement

Hi, I want to validate if the given input is a valid month (written in long month format) Jan / Feb / Mar / Apr / May / Jun etc etc This is what I've got with || (or statements): #!/usr/bin/ksh INPUT_DATE=$1 FORMATTED_DATE=`date | cut -f2 -d' '` #If there's no input use the... (1 Reply)
Discussion started by: Batsies
1 Replies

10. Shell Programming and Scripting

Validating the input date format

I have one script.for that the inputs are fromdate(dd/mon/yyyy) and todate(dd/mon/yyyy). How i can validate the input format?for eg.27/08/2008 is not valid.27/aug/2008 or 27/Aug/2008 are valid. and the todate is optional.so if the todate is not present in the input then i need to assign the... (6 Replies)
Discussion started by: Sharmila_P
6 Replies
Login or Register to Ask a Question