By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,849 Members | 2,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,849 IT Pros & Developers. It's quick & easy.

How to compare the date format?

P: 1
I am writing a programe in which user is asked to enter his/her date of birth in DD/MM/YYYY format. What if he/she enters the date in other format? How can I write a syntax to check whether he/she enters the date in the given format? Can somebody give me an exact idea?
Thanks
Jul 8 '06 #1
Share this Question
Share on Google+
6 Replies


Banfa
Expert Mod 5K+
P: 8,916
You check that the / are in the correct place in the string and you check that all the values entered are sensible, i.e. MM is in range 1 - 12 and DD is in correct range given MM.

You will never be able to completely verify a date entered in this format, for instance is someone accidentally enters 6th March 2006 in american format (MM/DD/YYYY) or 03/06/2006 there is no way your program can know this, it will just treat it as 3rd June 2006. Put up a prompt telling the user what format to use.
Jul 10 '06 #2

P: 35
I am writing a programe in which user is asked to enter his/her date of birth in DD/MM/YYYY format. What if he/she enters the date in other format? How can I write a syntax to check whether he/she enters the date in the given format? Can somebody give me an exact idea?
Thanks
You write the program which doesn't receive the date at a single stretch, that is first you prompt the user to enter DD, if he enters that then prompt him to enter MM and after that prompt him to enter YYYY.
Example :
Enter the Day =
if he enters the above value, then prompt him for
Enter the month =
and then
Enter the year =
Collect all the value in whichever format you want and thus the above problem will be solved.
Jul 12 '06 #3

P: 2
I agree with Ashish_CPP in that if the input is forced the right way much of the problem is solved. For example If you give an html field for instance with labels DD<field> , MM<field>, YYYY<field> and they put the wrong days into the wrong fields that's their mistake. You don't know what their date is ahead of time. I think your question relates to them entering DD-MM-YYYY instead of the / ? if so you can search some regular expresions for this you might be able to solve SOME cases but not all.
Jul 12 '06 #4

Banfa
Expert Mod 5K+
P: 8,916
If you are going complicate things by asking for separate fields then you may as well go the who hog and complicate things by getting the user to input the month as text or a text abrieviation (November or Nov).

If you are using a GUI this works well because you can use drop down lists to contain the data so you have control of the strings in use as well.

Once the month is a name not a number the date is unambiguous.
Jul 12 '06 #5

P: 35
I think even if you are not using GUI application, the logic that I gave in my previous reply will work fine, the advantage is that the you can collect the user input in an array and then print wherever and in whichever format you want. you can even change the format at run time. isn't it ?
Jul 12 '06 #6

Banfa
Expert Mod 5K+
P: 8,916
I think even if you are not using GUI application, the logic that I gave in my previous reply will work fine, the advantage is that the you can collect the user input in an array and then print wherever and in whichever format you want. you can even change the format at run time. isn't it ?
Yes :D :p :)
Jul 12 '06 #7

Post your reply

Sign in to post your reply or Sign up for a free account.