Click to See Complete Forum and Search --> : Date string
Skyler
May 6th, 2006, 05:30 PM
Hi,
I am wondering if anyone could help me with a conversion. I have a date that is in the format of
string date = "19-JAN-2006";
I need to add one day to it.
Does anyone have any idea?
Thanks in advance!
dcjr84
May 6th, 2006, 06:43 PM
I think it might be easier if didn't store the whole thing in one big string.
Something like this...
string day = "19";
string month = "January";
string year = "2006";
I say this because you are going to need to address each piece of data separately anyways. Like if the day is 31-January-2006 and you add one day to it, you will need to change it to 01-February-2006. You will also need to know how many days are in each month, account for leap years, etc, etc. :)
Skyler
May 6th, 2006, 06:47 PM
Hi,
Thanks for the reply. Unfortunately, I have no choice in the format. It is what it is being passed to me and I have to work with it. Any ideas?
Thanks again!
RoboTact
May 6th, 2006, 08:16 PM
Convert to any date object, add that day using correct operation on that object and print result. Don't try to add a day manually - you'll miss something.
Skyler
May 6th, 2006, 08:22 PM
Hi,
I am sorry but I am not familiar with the date object, could you give me a sample?
Thanks Much.
RoboTact
May 7th, 2006, 08:30 AM
I'm not familiar with that too, maybe someone here will answer better. There is standard time_t - time in seconds from some implementation-defined moment. You can convert to it from struct tm - calendar time with fields for year, day, month, etc. using make_time function, add 1 day (add 24*60*60 seconds), convert back and print. It's in C standard library. Bad advice, I'm afraid as there are days with different amount of seconds, maybe safer to add some 30 hours instead of 24.
dcjr84
May 7th, 2006, 01:02 PM
You could do something like this. Keep in mind though that I have not done
any checking for correct number of days in month, carrying over into a new
month, leap years, etc, etc.
#include <iostream>
#include <cstring>
#include <sstream>
using namespace std;
string addDay( string date );
int main( int argc, char* argv[] ){
string date = "19-JAN-2006";
string newDate = "";
newDate = addDay( date );
cout << newDate << endl;
cin.get();
return 0;
}
//This is the function you would write
//which takes a string date as an argument
string addDay( string Date ){
string dateArray[ 3 ];
string nDate = "";
int element = 0;
int iDay, iYear;
stringstream sDay, sYear;
//This will tokenize the string into
//three parts: day, month, year
string sep = "-";
string::size_type startoftoken = Date.find_first_not_of(sep, 0);
string::size_type endoftoken = 0;
while(startoftoken != string::npos){
endoftoken = Date.find_first_of(sep, startoftoken);
if(endoftoken==string::npos)
endoftoken = Date.length();
dateArray[ element++ ] = Date.substr(startoftoken, endoftoken-startoftoken);
startoftoken = Date.find_first_not_of(sep, endoftoken);
}
//Here is where you would manipulate the date
//I.e. make sure it has the correct number of
//days, if its a leap year, etc., etc.
//dateArray[0] == day
//dateArray[1] == month
//dateArray[2] == year
//Convert day and year from string to integer
iDay = atoi( dateArray[0].c_str() );
iYear = atoi( dateArray[2].c_str() );
//Add one day
iDay++;
//Add one year, if it was 31-DEC-2006
//iYear++;
//Convert day and year from integer to string
sDay << iDay;
dateArray[0] = sDay.str();
sYear << iYear;
dateArray[2] = sYear.str();
//Concatenate new string
nDate = dateArray[0] + "-" +
dateArray[1] + "-" +
dateArray[2];
//Return our new date
return nDate;
}
This information will also help you when doing changes to your dates:
How to calculate a leap year (http://www.codetools.com/cpp/LeapYear.asp)
Number of days in each month
January - 31
February - 28 ( 29 in a leap year )
March - 31
April - 30
May - 31
June - 30
July - 31
August - 31
September - 30
October - 31
November - 30
December - 31
Skyler
May 7th, 2006, 03:43 PM
Hi,
Thanks. This is EXACTLY what I am trying to do. Although, I am disappointed in myself for not getting it first, I hope with practice, I will get better.
~Thanks to All!
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.