How to substract one day to current time ?

 

I want to substract a day in:

string strTime = TimeYear(TimeCurrent()) + "." + TimeMonth(TimeCurrent()) + "." + TimeDay(TimeCurrent()) + " " + '22:41';

TheDate = StrToTime(strTime) - PERIOD_D1 ;

Alert(TimeToStr(TheDate));

If current day is 30 strTime = "2012.05.30 22:41" and TheDate should give "2012.05.30 22:41" but it isn't the case !

So HOW can I actually substract one day ?

 

forexgenuine 2012.05.30 07:53

I want to substract a day in:

string strTime = TimeYear(TimeCurrent()) + "." + TimeMonth(TimeCurrent()) + "." + TimeDay(TimeCurrent()) + " " + '22:41';

TheDate = StrToTime(strTime) - PERIOD_D1 ;

Alert(TimeToStr(TheDate));

If current day is 30 strTime = "2012.05.30 22:41" and TheDate should give "2012.05.30 22:41" but it isn't the case !

So HOW can I actually substract one day ?

Believe or not one day in MQL4 is

Alert (TimeToStr ((Time[0] - Time [1]), TIME_DATE|TIME_SECONDS)); // on daily chart
//==========================
datetime One_Day  = StrToTime ("1970.01.02 00:00:00"); // this is what mql4 say about one day

Try it and update us with the result.

 

You could subtracts this one day instead . . .

datetime OneDay = PERIOD_D1;  //  this is one day

datetime

 
forexgenuine:

I want to substract a day in:

string strTime = TimeYear(TimeCurrent()) + "." + TimeMonth(TimeCurrent()) + "." + TimeDay(TimeCurrent()) + " " + '22:41';

TheDate = StrToTime(strTime) - PERIOD_D1 ;

Alert(TimeToStr(TheDate));

If current day is 30 strTime = "2012.05.30 22:41" and TheDate should give "2012.05.30 22:41" but it isn't the case !

So HOW can I actually substract one day ?

PERIOD_D1 represents the number of minutes in a day ( = 1440 ) whilst datetime variables and calculations are based on seconds. If you want to subtract 1 day from another datetime value then just subtract (PERIOD_D1 * 60) which is 1440 * 60 or 86400, the number of seconds in a day.

Regards, Paul.

 
RaptorUK:

You could subtracts this one day instead . . .

datetime OneDay = PERIOD_D1 * 60;

 
paul_hughes:

PERIOD_D1 represents the number of minutes in a day ( = 1440 ) whilst datetime variables and calculations are based on seconds. If you want to subtract 1 day from another datetime value then just subtract (PERIOD_D1 * 60) which is 1440 * 60 or 86400, the number of seconds in a day.

Regards, Paul.

LOL . . oops, well spotted :-)
 

LOL

TimeToStr (PERIOD_D1 * 60, TIME_DATE|TIME_SECONDS) == "1970.01.02 00:00:00"

Just like me reply above.

 
forexgenuine:
I want to substract a day in:
  1. Do you really? What if yesterday was the weekend or a holiday?
    datetime now = TimeCurrent(),
             yesterday = now - 86400; // PERIOD_D1 * 60

  2. If you want the last trading datetime
    datetime now = TimeCurrent(),
             tod = now % 86400,
             yesterday = iTime(NULL, PERIOD_D1, 1),
             todYesterday = yesterday + tod;

Reason: