datetime to Epoch??

 

hi everyone

 

do you know some way to convert a date '09.09.2014 18:32:47'  or the current time and date to Epoch???

 

thanks for help

 

yhoyo 

 

Never try

TimeLocal()

?

 
deysmacro:

Never try

?


HI

 

No really.. that show the local time  (pc time).... but not the Espoch time :(

 
YHOYO:

hi everyone

 

do you know some way to convert a date '09.09.2014 18:32:47'  or the current time and date to Epoch???

 

thanks for help

 

yhoyo 

 

 

A datetime is Epoch.
 
angevoyageur:
A datetime is Epoch.


but  for example I do this:  Print("year: ", 2014*31556926);

 

the result is :  year: -868860476

and a error integral constant overflow

 

obviously the result should be year:  63555648964   

 

some ideas?? 

 
https://docs.mql4.com/dateandtime
 
deysmacro:
https://docs.mql4.com/dateandtime


hi.. thanks for this.... the clue is the #property strict

 

 datetime date=D'2014.03.05 15:46:58';
  string str="mydate="+date;
//--- str="mydate=1394034418" - without #property strict

//--- str="mydate=2014.03.05 15:46:58" - with #property strict 

 

 thanks again

 

With property strict.

#property strict
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
void start()
  {
   datetime date=D'2014.03.05 15:46:58';
   string str="mydate="+StringFormat("%i",date);
   Print(str);
  }
 
yhoyo:

hi everyone

 

do you know some way to convert a date '09.09.2014 18:32:47'  or the current time and date to Epoch???

 

thanks for help

 

yhoyo 


its very simple

void UnixTimeStamp()

  {

//---

  

   string unixTimestamp = (TimeCurrent()-0);

   int trk=StrToInteger(unixTimestamp);

   Print(" Epoch TimeStamps= ",trk);

  }

 

As @Alain Verleyen already said (over 2  years ago), datetime is epoch. There is no need for several lines of code 

Print((long)TimeCurrent());
 
yhoyo: do you know some way to convert a date '09.09.2014 18:32:47'  or the current time and date to Epoch???

Current time is easy TimeCurrent.

If you just have a constant convert it

datetime date=D'2014.03.05 15:46:58';

For the general case, StringToTime requires "yyyy.mm.dd hh:mi" so just reformat it:

string MDY_to_YMD(string mdy){
   string mm   = StringSubstr(mdy,  0, 2);   // 09.09.2014 18:32:47
   string dd   = StringSubstr(mdy,  3, 2);   // 012345678 0 2345678
   string yyyy = StringSubstr(mdy,  6, 4);
   string hms  = StringSubstr(mdy, 11, 8);   // yyyy.mm.dd hh:mi:ss
   return StringFormat("%s.%s.%s %s", yyyy, mm, dd, hsm);
}
datetime mdy_to_time(string mdy){
  return StringToTime( mdy_to_time(mdy) );
}
string   ymd  = "09.09.2014 18:32:47";
datetime when = MdyToTime(ymd); // D'2014.03.05 15:46:58'

Then you can do what you want with it. If you want the number long(when)


Alain Verleyen:
string str="mydate="+StringFormat("%i",date);
This post was pre-build 600. Since then datetime's are longs. so the formatter is now wrong. Should be "%ill" (eye, ell, ell) or "%iI64" (eye, cap eye 64.)
Reason: