Solved

Date Validation Function

Posted on 2004-04-06
16
260 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 how to use strings and some functions related to them in the C programming language.

758 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

24 Experts available now in Live!

Get 1:1 Help Now