Converting input dd/mm/yyyy to a full length date

Started by
4 comments, last by O_Joe 20 years ago
I need to convert an input such as 12/05/2001 into "The date is May 12th, 2001". i am new at programming and in desperate need of help. Does anyone have any examples or basic programs that work because i keep getting stuck with the "/" in between the numbers and also the variable inputs wont register with the right value in the cin expression plzzzz help [edited by - O_Joe on March 27, 2004 12:17:07 AM]
Advertisement
Other than some functions that may be provided by an API, it''
s relatively easy to do this yourself.

To convert the month from numeric to a string is trivial, you can do this by an array lookup. You take an array with the months'' names in it and there you go.

To compute the current day of the week is not as trivial, you''ll want to look up the eternal calendar in a numerics textbook. It''s not very complicated but still somewhat interesting.
I had a similar problem and I kept stumbling with the fact that the user enters the date with /''s... I *had* to use cin and it would''ve been fine if there could have been spaces but it didn''t - so I got the shits too much and left it.

Wanna tell me how to do it ShadowDancer?
I guess the hardest probelms would be to get the numbers for year, month and day from the input. I'd say if you absolutely have to use console or text field input, tell the user what format you prefer. YYYY-MM-DD is a standard, so something like "Please enter date (YYYY-MM-DD):" should be an acceptable prompt. You then split the input (do some verification for length, find the dashes, complain if it doesn't fit) and then put the atoi results from the respective substrings to the variables year, month and day. Then you do some more checking:

month between 1 and 12,
day between 1 and 31 with some checking for leap years

Leap years occur when year%4 == 0 and year%400 != 0, of course all this is only valid for the current "western" Gregorian calendar that is valid since 1582.

The eternal calendar is a funny thing that is relatively easy to understand but one hell to explain. Let's write the year a 100*c+d, where 0<=d<100. Now, if we write our date as (n.m.)100c+d with n==day and m==month, we get:

(n.m.)100c+d = (n + 5c + d + [(13m-1)/5] + [d/4] + [c/4]) % 7

with [] rounding to next integer. In that result, 0 is sunday, 1 monday and so on.

EDITS: too much theory copied from the book, 0 is of course sunday.


[edited by - Shadowdancer on March 27, 2004 6:22:21 PM]
strptime and strftime.
Get to know this site: www.snippets.org

here is the links to answer your question.

http://cpp.snippets.org/snip_lister.php?fname=zdate.man

http://cpp.snippets.org/snip_lister.php?fname=date.hpp

http://cpp.snippets.org/snip_lister.php?fname=date.cpp

http://cpp.snippets.org/snip_lister.php?fname=datedemo.cpp

have fun...

This topic is closed to new replies.

Advertisement