Hello, please, I need suggestion for add a month in a date variable. Example:
maybe you can do something with StringSubstr
Hello, please, I need suggestion for add a month in a date variable. Example:
string date = "1992.01.01"; datetime newDate = StrToTime(date)+1 month; Print(TimeToStr(newDate)); //Result: 1992.02.01
Something like this . . .
string sDate = "1992.01.01"; string sNewDate; datetime tDate, tNewDate); int NewYear, NewMonth; tDate = StrToTime(sDate); Print( TimeToStr(tDate, TIME_DATE) ); if(TimeMonth(tDate) == 12) { NewMonth = 1 NewYear = TimeYear(tDate) + 1; } else { NewMonth = TimeMonth(tDate) + 1; NewYear = TimeYear(tDate); } sNewDate = StringConcatenate(NewYear, ".", NewMonth, ".", TimeDay(tDate)); //Result: 1992.02.01
Something like this . . .
I like your solution but i don't know exactly what the OP means with add a month in a date variable
the problem we get doing it is when we have a day in our string that is not inside the new month
i mean suppose date variable string sDate = "1992.01.31";
februari hasn't day 31 so i see more problems adding a month to a day....
Hello guys, thank you very much for the help. The Raptor's solution works for me. I agree with deVries, that we can have some problem when using day 30 or 31, but in my case this will not happen because I'm working with monthly timeframe and always start with day 1. By the way, the feature "Subscribe to topic" of the forum isn't working, I haven't received a e-mail with your answers. I had to come checking the topic.
I like your solution but i don't know exactly what the OP means with add a month in a date variable
the problem we get doing it is when we have a day in our string that is not inside the new month
i mean suppose date variable string sDate = "1992.01.31";
februari hasn't day 31 so i see more problems adding a month to a day....
I ran a small test using the following code:
datetime nDate = 0, cDate = D'2013.01.31 00:00'; nDate = StrToTime(StringConcatenate(TimeYear(cDate), ".", TimeMonth(cDate)+1, ".", TimeDay(cDate))); Print ("Original Date: ", TimeToStr(cDate)); Print ("Modified Date: ", TimeToStr(nDate));
It produced the following output in the log:
It appears that MQL corrects the problem that you have raised.
I like your solution but i don't know exactly what the OP means with add a month in a date variable
the problem we get doing it is when we have a day in our string that is not inside the new month
i mean suppose date variable string sDate = "1992.01.31";
februari hasn't day 31 so i see more problems adding a month to a day....
It appears that MQL corrects the problem that you have raised.
By the way, the feature "Subscribe to topic" of the forum isn't working, I haven't received a e-mail with your answers. I had to come checking the topic.
Does it fix the year if you add a month to December ?
No, it doesn't appear so.
datetime nDate = 0, cDate = D'2012.12.01 00:00'; nDate = StrToTime(StringConcatenate(TimeYear(cDate), ".", TimeMonth(cDate)+1, ".", TimeDay(cDate))); Print ("Original Date: ", TimeToStr(cDate)); Print ("Modified Date: ", TimeToStr(nDate));
Ah . . . good point, but that should be easy to address, perhaps the OP can address it
Like this . . .
string sDate = "1992.01.01"; string sNewDate; datetime tDate, tNewDate; int NewYear, NewMonth, NewDay; tDate = StrToTime(sDate); Print( TimeToStr(tDate, TIME_DATE) ); // test if day of new month will be valid if(TimeMonth(tDate) == 11) { NewMonth = 1 NewYear = TimeYear(tDate) + 1; } else if(TimeMonth(tDate) == 12) { NewMonth = 2 NewYear = TimeYear(tDate) + 1; } else { NewMonth = TimeMonth(tDate) + 2; NewYear = TimeYear(tDate); } tNewDate = TimeToStr( StringConcatenate( NewYear, ".", NewMonth, ".01" ), TIME_DATE); if ( TimeDay(tDate - (PERIOD_D1 * 60) ) < TimeDay(tDate) ) // test if the last day of the next month is < the current day of the month NewDay = TimeDay(tDate - (PERIOD_D1 * 60) ); if(TimeMonth(tDate) == 12) { NewMonth = 1 NewYear = TimeYear(tDate) + 1; } else { NewMonth = TimeMonth(tDate) + 1; NewYear = TimeYear(tDate); } sNewDate = StringConcatenate(NewYear, ".", NewMonth, ".", NewDay); //Result: 1992.02.01
. . . not tested or compiled.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello, please, I need suggestion for add a month in a date variable. Example: