Solved

Date Validation Function

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

Accepted Solution

by:
Kent Olsen 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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 conditional statements in the C programming language.

691 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