Unix date validation


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Unix date validation
# 1  
Old 12-22-2010
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 just 1 to it in every iteration, yes functionally it works fine. But there is a unnecessary folder check for files like 20101235 the reason is that I just used it as a number, so it just increments without realizing it is not a valid date, no harm but it is unnecessary.
So I need to validate whether it is valid date J I wrote a small script to do it as below.

But if there a single function to do this already, please let me know.
Or any better suggestions?

Code:
f_HandleDate()
{

  #Split the date into Year month and Date. Format YYYYMMDD.
       year=`echo $NEXTRUNDATE | cut -c1-4` 
       month=`echo $NEXTRUNDATE | cut -c5-6`
       date=`echo $NEXTRUNDATE | cut -c7-8`

  #Use standard as maximum 31 days for all the months. eg., Feb month will also be considered as 31 days but not an issue here.     
       if [ "$date" -gt 31 ]
       then   
              month=`expr $month + 1`  #If date > 31 then increase month by 1.

              if [ "$month" -gt 12 ]
              then
                     month=1                 # If month  > 12 then reset value to 1.
                     year=`expr $year + 1`      # Increment year value by 1.
              fi

              if [ "$month" -lt 10 ]
              then
                     month=`echo "0"$month`  #if month < 10 then prefix 0. eg., 8 will be changed to 08
              fi

               NEXTRUNDATE=`echo $year$month"01"` # Form the new date
         #echo "New value $NEXTRUNDATE"
       fi



Regards,
Narayanan K


Moderator's Comments:
Mod Comment Please use code tags when posting data and code samples!

Last edited by Franklin52; 12-22-2010 at 07:39 AM..
# 2  
Old 12-22-2010
If u have perl installed in your machin then use following simple code.

Code:
#!/usr/bin/perl -w
# Name   : isValidDate
# Purpose: To validate given date 
# Input  : Date(YYYYMMDD)
# Output : display "Invalid Date" if invalid, "Valid Date" if valid 
#
    my $inputdate =$ARGV[0];
    return "Invalid Date" unless length($inputdate)==8;
    return "Invalid Date" unless (Date::DateCalc::check_date(substr($inputdate,0,4),
                                                    substr($inputdate,4,2),
                                                    substr($inputdate,6)));
    return "Valid Date";

Execute:

Code:
$ perl dateValidate.pl 20101222
Valid Date
$ perl dateValidate.pl 20101235
Invalid Date

R0H0N
# 3  
Old 12-22-2010
Hi thx for ur reply.. That wud really b helpful. . I wud also like to hav perless solution
 
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. Shell Programming and Scripting

Validation of date from file name

I'm writing a shell script for cleanup of older files from various sub-directories inside a main directory The structure of directories is as below: Logs daily online archive weekly online archive... (1 Reply)
Discussion started by: asyed
1 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. 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

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

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

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

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

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

10. Shell Programming and Scripting

Problem with Date validation

Hi All, I've to validate a date input for the format YYYYMMDD. The input should be of 8 digits with only numeric values. I tried the following echo $1 | grep '^\{8}$/p' if then echo "Valid" else echo "invalid" I get the error, grep: RE error 16: Bad number. Any help... (3 Replies)
Discussion started by: sumesh.abraham
3 Replies
Login or Register to Ask a Question