473,385 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Converting date string to a number

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
3 7111
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Kim Hellan | last post by:
I have a simple string in the format "DD-MM-YY hh:mm:ss", that I need to convert to a DateTime value. I know this is a standard problem, but please don't just link to all the MSDN pages regarding...
1
by: Glenn M | last post by:
I have a date stored as a string in the format mm/dd/yyyy. What is the easiest way to get this date converted to a datetime object so i can include it in the datediff function. also what is the...
29
by: james | last post by:
I have a problem that at first glance seems not that hard to figure out. But, so far, the answer has escaped me. I have an old database file that has the date(s) stored in it as number of days. An...
3
by: NateM | last post by:
How do I convert any given date into a milliseconds value that represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT? Is there an easy way to do this like...
16
by: Bishman | last post by:
This is driving me mad, should be simple, How can I format a date string "01122006" to be "01/12/2006" I have tried string teststr = String.Format("{d}","01122006"); AND string teststr =...
17
by: Petyr David | last post by:
Just looking for the simplest. right now my perl script returns an error messge to the user if the date string is invalid. would like to do this before accessing the server. TX
2
by: Brian Parker | last post by:
I am beginning to work with VB2005.NET and I'm getting some problems with string formatting converting an application from VB6. VB6 code:- sTradeDate = Format(pArray(4,i Record), "mmddyy") ...
2
by: stainless | last post by:
I know this is probably simple but I cannot find a method of converting a date string into a format that matches the DatePicker format in C# eg string "20080131" converted to "31 January 2008" ...
11
by: davidj411 | last post by:
i am parsing a cell phone bill to get a list of all numbers and the total talktime spend on each number. i already have a unique list of the phone numbers. now i must go through the list of...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.