Solved

Date Validation Function

Posted on 2004-04-06
16
261 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
  • 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 45

Accepted Solution

by:
Kdo earned 200 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
 
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now