Date arithmetic

 

I know I can determine the number of days between two dates by simply subtracting the dates...but how do I determine the date that is n-days from another date?

TIA...Chuck

 
ChuckM:

I know I can determine the number of days between two dates by simply subtracting the dates...but how do I determine the date that is n-days from another date?

Subtracting one date from another - e.g. an OrderOpenTime() from TimeCurrent() - gives you a number of seconds, not a number of days. To work out the number of complete days between two dates, you simply divide the difference in seconds by 86,400 (60 sec * 60 mins * 24 hours). Similarly, adding 86,400 to a timestamp advances it by one day. Therefore, for example, 7 days ago would be TimeCurrent() - 604800.

 
jjc:

Subtracting one date from another - e.g. an OrderOpenTime() from TimeCurrent() - gives you a number of seconds, not a number of days. To work out the number of complete days between two dates, you simply divide the difference in seconds by 86,400 (60 sec * 60 mins * 24 hours). Similarly, adding 86,400 to a timestamp advances it by one day. Therefore, for example, 7 days ago would be TimeCurrent() - 604800.

Thanks for the clarification. I was unable to find any documentation that said what the result of subtracting two datetime values would be. Through trial and error, I found that I can place the result into another datetime field from which I can get the number of days.

 
ChuckM:

I was unable to find any documentation that said what the result of subtracting two datetime values would be.

Its written in the reference manual: https://docs.mql4.com/basis/types/datetime, the documentatiion of the date and time functions also mentions it several times: https://docs.mql4.com/dateandtime, https://docs.mql4.com/dateandtime/TimeCurrent, https://docs.mql4.com/dateandtime/TimeLocal, the datetime type is an integer containing an ordinary UNIX Timestamp.

 
7bit:

Its written in the reference manual: https://docs.mql4.com/basis/types/datetime, the documentatiion of the date and time functions also mentions it several times: https://docs.mql4.com/dateandtime, https://docs.mql4.com/dateandtime/TimeCurrent, https://docs.mql4.com/dateandtime/TimeLocal, the datetime type is an integer containing an ordinary UNIX Timestamp.

Thanks, bit. I was actually looking for some guidance in the arithmetic...in my frustration, I overlooked the obvious. It's been a couple of decades since I programmed in a language that did not have ADDDATE() type functions.