Date and Time Calculation How To???

 

How to Calculate with date and times?

On several places there are comments like the following (https://www.mql5.com/en/forum/688): 

datetime t;
MqlDateTime mdt;
TimeToStruct(t,mdt); 

mdt.day += 45;
//or mdt.day = 50; //50 days from the beginning of the month
//or mdt.hour += 134; //134 hours in the future
//or mdt.hour = 1000; //1000 hours from the beginning of the day 


This works but gives incorrect results. example:
mdt.day -= 50 gives an negative day and not a day one ore two month in the past.

What is MqlDateTime useful for if not for calculations?
and how do you calculate valid dates?

Attached is a indicator that shows the incorrect behavior of the above method

regards and thanks for your help
Daniel 

datetime calculations
  • www.mql5.com
Is there a way to get an open time of a weekly bar for instance, given a day and time on that week?
Files:
datetest.mq5  4 kb
Reason: