Increment date

 

Hi all

Does anyone know a way to increment a date easily without having to create specific functions? I have tried searching the standard codebase but struggling to find anything.

E.g. If today's date is "2022.11.18", I want to return a date of -1 day increment, e.g. "2022.11.17".

It would be similar to the Datetime.AddDays(int as dayIncrement) function in c#.

Thanks,

 
anupmistry123: Does anyone know a way to increment a date easily without having to create specific functions? I have tried searching the standard codebase but struggling to find anything. E.g. If today's date is "2022.11.18", I want to return a date of -1 day increment, e.g. "2022.11.17". It would be similar to the Datetime.AddDays(int as dayIncrement) function in c#. Thanks,

"The datetime type is intended for storing the date and time as the number of seconds elapsed since January 01, 1970."

To decrement by 1 day, just subtract 24 hours x 60 minutes x 60 seconds.

datetime dtBefore = TimeCurrent() - 24 * 60 * 60;
 
anupmistry123: E.g. If today's date is "2022.11.18", I want to return a date of -1 day increment, e.g. "2022.11.17".

Per your request, Fernando has answered.

But you, probably, don't want yesterday, but the last trading date.
          Find bar of the same time one day ago - MQL4 programming forum #1 & #6 (2017)

 
anupmistry123:

Hi all

Does anyone know a way to increment a date easily without having to create specific functions? I have tried searching the standard codebase but struggling to find anything.

E.g. If today's date is "2022.11.18", I want to return a date of -1 day increment, e.g. "2022.11.17".

It would be similar to the Datetime.AddDays(int as dayIncrement) function in c#.

Thanks,

Use the Datetime class

#include <Tools\DateTime.mqh>
CDateTime example;

example.Date(D'18.11.2022');
Print(" Date before: ", example.DateTime());
example.DayDec();
Print(" Date After decrement: ", example.DateTime());
 
Thanks all, great will try all of these solutions. Appreciate the concise answers!!
Reason: