How to use MQL5 CDateTime library to add 1 or 2 months to a date

 
I'm trying to add 1 and 2 months to date, but all solutions I find online are about MQL4, which doesn't work on MQL5 compiler. Can anyone suggest an MQL5 way of adding months or days to a date?
Thank you.
 
Use the MonInc method. It's in the documentation.

MonInc

Adds specified number of months


void  MonInc(
   int   delta=1        // months
   )
 
Martian4x:
I'm trying to add 1 and 2 months to date, but all solutions I find online are about MQL4, which doesn't work on MQL5 compiler. Can anyone suggest an MQL5 way of adding months or days to a date?
Thank you.
This wont work: newTime = time + PeriodSeconds(PERIOD_MN1) ?
 
Carl Schreiber #: This wont work: newTime = time + PeriodSeconds(PERIOD_MN1) ?
No, because months and years do not have fixed duration in seconds, unlike minutes, hours, days, or weeks which do have fixed duration in seconds
 
Fernando Carreiro #:
Use the MonInc method. It's in the documentation.

MonInc

Adds specified number of months


Can you please provide an example code on how to perform addition? Newbie here.
 
Martian4x #: Can you please provide an example code on how to perform addition? Newbie here.

You should first learn how to code in the MQL Object Oriented Programming (OOP) style. By that I mean, how to code with classes and objects. Once you know that, you will not need an example.

The MQL documentation about OOP is not very "user friendly" in my opinion, so search the web for tutorials on C++ OOP which is on what MQL OOP is based on.

Documentation on MQL5: Language Basics / Object-Oriented Programming
Documentation on MQL5: Language Basics / Object-Oriented Programming
  • www.mql5.com
Object-Oriented Programming - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Well I figured it out. For anyone stuck here's what I did.

Declaration
#include
<Tools\DateTime.mqh>
struct CDateTime;
datetime StartDate, EndDate;

code:
      CDateTime newDate;
      TimeToStruct(StartDate,newDate);
      newDate.MonInc(1);
      EndDate = newDate.DateTime();
Reason: