?
Solved

Date Validation Function

Posted on 2004-04-06
16
Medium Priority
?
268 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.
0
Comment
Question by:local_kidu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +4
16 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10764505
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
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10764662
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
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 800 total points
ID: 10764949

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 12

Expert Comment

by:stefan73
ID: 10764995
Hi sunnycoder,
Did you kill my post?

Cheers,
Stefan
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10765047
sunnycoder,
OK.

Stefan
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10765105
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
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10765113
To simplify the string manipulation part,u can use strtok() as well.
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10765513
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
 
LVL 84

Expert Comment

by:ozo
ID: 10767913
Would it be legal homework help to suggest using mktime() for date validation?
0
 
LVL 2

Expert Comment

by:Avik77
ID: 10768360
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10772040
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
 

Expert Comment

by:msanoop
ID: 11251845
USE STRTOK(), Its recommended
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question