GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-May-2006, 17:20
Daunte Daunte is offline
New Member
 
Join Date: May 2006
Posts: 3
Daunte is on a distinguished road
Question

Trying to convert a Date String


Hi,

I am wondering if anyone could help me with a conversion. I have a date that is in the format of
Code:
string date = "19-JAN-2006";

I need to add one day to it.

Does anyone have any idea?

Thanks,
  #2  
Old 06-May-2006, 19:05
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: Trying to convert a Date String


well, since you know the format of the string, you can easily parse it into a number (usually computers represent date/time as offset since Jan 1, 1970). One question is with single-digit days, will the format be 1-JAN-1970 or 01-JAN-1970?
In any case, I would probably use a stringstream. Research these. You would declare the stringstream, output the string date to it, and then extract (>> operator) the day, month, and year. Then you could add one to the day, but make sure to handle cases where it spills over into the next month (you'll need to algorithm-ize "30 days hath september..." and leap years (a year is a leap year if it's divisible by 4, but not if it's divisible by 100 unless it's also divisible by 400; have fun concocting that if condition)). See how far you can get and post back here if you need further assistance.
  #3  
Old 06-May-2006, 20:51
Daunte Daunte is offline
New Member
 
Join Date: May 2006
Posts: 3
Daunte is on a distinguished road
Question

Re: Trying to convert a Date String


Hi,

OK I have been able to parse my string, and then convert the date into an int. Is there a way I can use some sort of date class and pass in my parameters??

CPP / C++ / C Code:
int main ()
{
  char str[] ="12-DEC-2006";
  char * pch;
  int i=0;
  char * datestring[3];
  int addoneday;

  pch = strtok (str,"-");

     while (pch != NULL)
     {
       datestring[i] = pch;
       printf ("%s\n",datestring[i]);
       pch = strtok (NULL, "-\n");
       i++;
     }

  addoneday = atoi(datestring[0]);

Thanks,
Last edited by LuciWiz : 07-May-2006 at 09:11. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #4  
Old 06-May-2006, 21:20
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Trying to convert a Date String


Quote:
Originally Posted by Daunte
OK I have been able to parse my string, and then convert the date into an int. Is there a way I can use some sort of date class and pass in my parameters??
Now that it's an int can't you add one to the value? There is no "sort of date class" I'm aware of. Don't forget about ubergeek's suggestion...
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #5  
Old 06-May-2006, 23:31
Daunte Daunte is offline
New Member
 
Join Date: May 2006
Posts: 3
Daunte is on a distinguished road
Question

Re: Trying to convert a Date String


Hi,

Yes, actually for my debugging, I simply added a one to the number. However, as suggested, there are a few things to take account for, and using a bunch of if -else statements or quite a few nested for loops, I was hoping there was a cleaner way.


Thanks!
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Read a .html file, check that file for links salemite C Programming Language 10 17-Jan-2008 07:56
Convert date string to number bjvar C Programming Language 3 03-Apr-2006 13:32
variables return to previous value after i try to set them nasaiya MS Visual C++ / MFC Forum 2 14-Jun-2005 00:43
Help wit my source code compiler errors Krandygrl00 C++ Forum 1 06-Jun-2005 08:14
Converting string to float CT++ C++ Forum 2 10-May-2005 11:29

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:10.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.