We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Date Validation Function

local_kidu
local_kidu asked
on
Medium Priority
290 Views
Last Modified: 2010-04-15
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.
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2006

Commented:
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
If this is ur homework,U will have to do the work.
We will be glad to help u out if ur stuck somewhere.

Data Warehouse / Database Architect
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Top Expert 2004

Commented:
Hi sunnycoder,
Did you kill my post?

Cheers,
Stefan
Top Expert 2004

Commented:
sunnycoder,
OK.

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


To simplify the string manipulation part,u can use strtok() as well.
Top Expert 2004

Commented:
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
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
Would it be legal homework help to suggest using mktime() for date validation?
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.
CERTIFIED EXPERT
Top Expert 2006

Commented:
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

Commented:
USE STRTOK(), Its recommended
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.