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

Converting date string to a number

P: n/a
How can I convert a date string to a number (e.g. a time_t value or a tm
struct)? I know about the strptime function, but then I have to know the
format string. And that is a problem.

I'm trying to autoformat the contents of text entries in a GUI. For
numbers, I'm converting the text representation to the appropriate type
(using atoi, atof, ...) and converting the result back to text with the
correct format (using sprintf). But this does not work for date (or
time) strings.

For instance, if the desired date format is "dd-mm-yy", but the user
enters "dd-mm-yyyy", I can't convert the string to a valid date, because
I don't know its format.
Jul 18 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Jef Driesen" <je********@hotmail.com.invalidwrote in message
news:f7**********@ikaria.belnet.be...
How can I convert a date string to a number (e.g. a time_t value or a tm
struct)? I know about the strptime function, but then I have to know the
format string. And that is a problem.

I'm trying to autoformat the contents of text entries in a GUI. For
numbers, I'm converting the text representation to the appropriate type
(using atoi, atof, ...) and converting the result back to text with the
correct format (using sprintf). But this does not work for date (or time)
strings.

For instance, if the desired date format is "dd-mm-yy", but the user
enters "dd-mm-yyyy", I can't convert the string to a valid date, because I
don't know its format.
atoi, atof, etc.. are a pain in the neck. If you use stringstream it's
rather easy.

std::cout << "Enter Date:";
std::cin >Date;
std::stringstream Stream(Date);
int Month, Day, Year;
char Hyphen;

if ( Stream >Month >Hyphen >Day >Hyphen >Year )
// Got good date. Values in Month, Day and year. Should validate them
somehow
else
// Something went wrong, not in right format.

The good thing about this is, you don't have to care how long the month, day
or year entries are. They could enter a 1 or 100 digit number, and it would
read it. Of course, after this is done you want to do reality checking.
I.E. Is month 1-12? Is day 1-31? If year is 2 digit, do you want to add
1900 or 2000? If year is 4 digit, is it realistic? Etc...
Jul 18 '07 #2

P: n/a
On 2007-07-18 11:01, Jef Driesen wrote:
How can I convert a date string to a number (e.g. a time_t value or a tm
struct)? I know about the strptime function, but then I have to know the
format string. And that is a problem.

I'm trying to autoformat the contents of text entries in a GUI. For
numbers, I'm converting the text representation to the appropriate type
(using atoi, atof, ...) and converting the result back to text with the
correct format (using sprintf). But this does not work for date (or
time) strings.

For instance, if the desired date format is "dd-mm-yy", but the user
enters "dd-mm-yyyy", I can't convert the string to a valid date, because
I don't know its format.
Since you are using a GUI framework check if there is some kind of
date/time class/thingie which can parse time and dates. Standard C++ is
quite poorly equipped on this area but most frameworks have better support.

--
Erik Wikström
Jul 18 '07 #3

P: n/a
Erik Wikström wrote:
On 2007-07-18 11:01, Jef Driesen wrote:
>How can I convert a date string to a number (e.g. a time_t value or a tm
struct)? I know about the strptime function, but then I have to know the
format string. And that is a problem.

I'm trying to autoformat the contents of text entries in a GUI. For
numbers, I'm converting the text representation to the appropriate type
(using atoi, atof, ...) and converting the result back to text with the
correct format (using sprintf). But this does not work for date (or
time) strings.

For instance, if the desired date format is "dd-mm-yy", but the user
enters "dd-mm-yyyy", I can't convert the string to a valid date, because
I don't know its format.

Since you are using a GUI framework check if there is some kind of
date/time class/thingie which can parse time and dates. Standard C++ is
quite poorly equipped on this area but most frameworks have better support.
I'm using gtkmm (the C++ bindings for gtk+), but it doesn't seem to have
more advanced date/time functions.
Jul 19 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.