• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

Date Validation Function

Hello Experts,
            
            I am writing a database program in C. A situation arised where I wanted to validae the startdate and enddate. I am accepting the date in format(dd/mm/yyyy).
Can anyone get me a date validation function in C which does the following things

1. Check if the date is in correct format(dd/mm/yyyy)i.e; day shouldn't  be > 31, month shouldn't be > 12 ........
2. Check for characters( characters should not be present)
3. Start date should be lesser than end date

A code snippet/useful weblinks will be very useful. as I am not that versed in string manipulation functions in C

Thanks in Advance

NP.
0
local_kidu
Asked:
local_kidu
  • 3
  • 3
  • 2
  • +4
1 Solution
 
sunnycoderCommented:
Hi local_kidu,

>I am writing a database program in C.
Writing a database program in C when you are not very familiar with the library seems odd ... Is this your homework ?

Sunnycoder
0
 
ankuratvbCommented:
If this is ur homework,U will have to do the work.
We will be glad to help u out if ur stuck somewhere.

0
 
Kent OlsenData Warehouse Architect / DBACommented:

Most database engines have their own date check routines.  Since the database will verify the dates being passed to it, it makes for a simpler program on your end to simply pass the date to the database and let it do its own checking.  If the date is bad, an error will be returned to let you know.


Kent
0
KuppingerCole Reviews AlgoSec in Executive Report

Leading analyst firm, KuppingerCole reviews AlgoSec's Security Policy Management Solution, and the security challenges faced by companies today in their Executive View report.

 
stefan73Commented:
Hi sunnycoder,
Did you kill my post?

Cheers,
Stefan
0
 
stefan73Commented:
sunnycoder,
OK.

Stefan
0
 
ankuratvbCommented:
If u want to do this urself rather than letting the database engine do it.here are a few pointers:

1. Search the string in which the date is stored for a '/'(while coding u'll have to use '//')
and keep storing the characters till u dont get '/' in a temp array.Insert a '\0' in the temp array.U have the day in the temp array.Convert it to int by using atoi(temp)

2.Search the string from where u left and look for another '/' and storing chars in another temp array as in step 1.COnvert this to int as well.

3. Search the string from where u left and look for '\0'.Use similar procedure to obtain year as an integer as well.

Now u can perform ur checks for valid day,month and year.If month is feb.and day>28 check for leap year.


0
 
ankuratvbCommented:
To simplify the string manipulation part,u can use strtok() as well.
0
 
stefan73Commented:
local_kidu,

Hmmm, this would be a classing for Perl's regular expressions, like:

die "Wrong string format" unless $datestr =~ #(\d\d)/(\d\d)/(\d\d\d\d)#;
($day,$month,$year)=($1,$2,$3);

pity...

Stefan
0
 
ozoCommented:
Would it be legal homework help to suggest using mktime() for date validation?
0
 
Avik DasguptaCommented:
Hi,
  Please write a little code for ur work.
 Regarding string manipulation consult the manuals for string.h
 For date handling try to create a date structure and do the problem. u can represent the date by combining {day,month,year} values into a string value.

Avik.
0
 
sunnycoderCommented:
ozo,

It really depends on case to case basis but a post with
"man mktime
man strtok"
is generally acceptable.
It is bascially we, the peer experts, who draw the line

sunnycoder
0
 
msanoopCommented:
USE STRTOK(), Its recommended
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

  • 3
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now