Validate date format in argument


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Validate date format in argument
# 1  
Old 04-24-2009
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.
# 2  
Old 04-24-2009
date checks are not all that simple - here is a script that validates modern dates:
Code:
#!/bin/ksh
# usage: vdate.shl <date> where date = month/day/year
# valid for date = or > 10/04/1582 -- start of Gregorian calendar

isleapyr()
{
	isleap=0
	if [[ $(( ${1} % 400 )) -eq 0  ||
	      $(( ${1} % 4 )) -eq 0 && $(( ${1} % 100 )) -ne 0 ]]
	then
	   isleap=1
	fi
}

isvaliddate()
{
 	typeset -i day=$1
 	typeset -i month=$2
 	typeset -i year=$3
	set -A arr 31 28 31 30 31 30 31 31 30 31 30 31
	
 	if [[ $year -lt 34 ]] ; then  # assume 2000 
 		year=$(( $year + 2000 ))
 	fi
 	if [[ $year -gt 33 && $year -lt 100 ]] ; then  # assume 1900
 	    year=$(( $year + 1900 ))
 	fi
 	
 	isleapyr $year
    if [[ $isleap -eq 1 ]] ; then
    	arr[1]=29;
    fi
 
 	if [[ $month -gt 0 && $month -lt 13 ]] ; then
 		month=$(( $month - 1 ))
 		if [[ $day -gt 0 && $day -le ${arr[month]} ]] ; then
 			return 0
 		fi
 	fi
 	return 1
}

echo "$1" | tr -s '/' ' ' | read month day year
isvaliddate $day $month $year

if [[ $? -eq 0 ]] ; then
  echo "$1 ok"
else
  echo "$1 not ok"
fi

More sophisticated code turns the date given into Julian days (days since DEc 31 4713 BCE)
then converts the JD back to Gregorian. if the "converted back date" matches the one given then it passes the check.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 04-26-2009
below is not exactly what you want, but should throw some light on you.

Code:
IFS="/"
set $1
if [ $1 -lt 0 ] || [ $1 -gt 12 ];then
	echo "Error month"
elif [ $2 -lt 0 ] || [ $2 -gt 31 ];then
	echo "Error day"
else
	echo "hello $*"
fi

# 4  
Old 05-05-2009
Thanks all. I appreciate your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date: invalid date trying to set Linux date in specific format

i try to set linux date & time in specific format but it keep giving me error Example : date "+%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" or date +"%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" keep giving me this error : date: invalid date ‘19-01-2017 00:05:01' Please use CODE tags... (7 Replies)
Discussion started by: umen
7 Replies

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

3. UNIX for Dummies Questions & Answers

Rename all Files in a UNIX Directory from one date format to another date format

Hi Unix Gurus, I would like to rename several files in a Unix Directory . The filenames can have more than 1 underscore ( _ ) and the last underscore is always followed by a date in the format mmddyyyy. The Extension of the files can be .txt or .pdf or .xls etc and is case insensitive ie... (1 Reply)
Discussion started by: pchegoor
1 Replies

4. Shell Programming and Scripting

Need script to validate file according to date

Hi All, I am very new to unix and just started to work with unix and shell scripting.I have a query anyone help would be much appreciated I am using sun solaris OS I want to validate a file according to its date and if validate successful then it would write the file name,size,date and... (3 Replies)
Discussion started by: sv0081493
3 Replies

5. UNIX for Dummies Questions & Answers

Changing from Excel date format to MySQL date format

I have a list of dates in the following format: mm/dd/yyyy and want to change these to the MySQL standard format: yyyy-mm-dd. The dates in the original file may or may not be zero padded, so April is sometimes "04" and other times simply "4". This is what I use to change the format: sed -i '' -e... (2 Replies)
Discussion started by: figaro
2 Replies

6. Shell Programming and Scripting

convert date format to mysql date format in log file

I have a comma delimited log file which has the date as MM/DD/YY in the 2nd column, and HH:MM:SS in the 3rd column. I need to change the date format to YYYY-MM-DD and merge it with the the time HH:MM:SS. How will I got about this? Sample input 02/27/09,23:52:31 02/27/09,23:52:52... (3 Replies)
Discussion started by: hazno
3 Replies

7. Shell Programming and Scripting

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... (2 Replies)
Discussion started by: rajugp1
2 Replies

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

9. Shell Programming and Scripting

Validate date

Hi I am beginner to UNIX. I wanted to help for how to write script to validate date. Please help me. (2 Replies)
Discussion started by: giridher2000
2 Replies

10. Shell Programming and Scripting

format date by argument

Hello, I am currently writing a script that accepts an argument (month/date/year) when you run it. example: "./spell_date 03/12/90" I have gotten the 03/12/90 stored into variables and all, but I was wondering how i could print it out so that it would output: March, 12, 1990. Is there a way to... (1 Reply)
Discussion started by: MrGoldfinger
1 Replies
Login or Register to Ask a Question