Hi,
I am wondering if anyone could help me with a conversion. I have a date that is in the format of
I need to add one day to it.Code:string date = "19-JAN-2006";
Does anyone have any idea?
Thanks in advance!
Printable View
Hi,
I am wondering if anyone could help me with a conversion. I have a date that is in the format of
I need to add one day to it.Code:string date = "19-JAN-2006";
Does anyone have any idea?
Thanks in advance!
I think it might be easier if didn't store the whole thing in one big string.
Something like this...
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. :)Code:string day = "19";
string month = "January";
string year = "2006";
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!
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.
Hi,
I am sorry but I am not familiar with the date object, could you give me a sample?
Thanks Much.
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.
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.
This information will also help you when doing changes to your dates:Code:#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;
}
How to calculate a leap year
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
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!