TimeToStr() is the easy option.
GlobalVariableSet("D1_Bar",iTime(NULL,1440,1)); Print(TimeToStr(GlobalVariableGet("D1_Bar")));
Alternatively, you can get imaginative combining the components into a string of your format choice.
But if you do it this way, GlobalVariableSet is a datetime and GlobalVariableGet is a double.
Using TimeToStr would give a "possible loss of data due to type conversion" which i'd rather not use this way?
datetime is a ulong variable double variable aren't that exact so if you store a date as double and reconvert back to datetime you risk to get another date+time than the one you've originally saved,
but may be the date you save could be 'small' enough to get back the same date+time.
Thats interesting thanks gooly.
I think I may test this under the circumstances in which I use double and datetime. I really want to store the datetime so that if MT4 crashes or restart happens, it will continue from where it left off (as it uses datetime to the hour precision - not minutes or seconds etc.).
How could I go about properly displaying this in the Comment so that I can read it then without having: "possible loss of data due to type conversion"
How could I go about properly displaying this in the Comment so that I can read it then without having: "possible loss of data due to type conversion"
Typecast it:
GlobalVariableSet("D1_Bar",iTime(NULL,1440,1)); Print(TimeToStr((datetime)GlobalVariableGet("D1_Bar")));
You can find out more about typecasting, and the limitations that Gooly has raised, here.
But if you do it this way, GlobalVariableSet is a datetime and GlobalVariableGet is a double.
Using TimeToStr would give a "possible loss of data due to type conversion" which i'd rather not use this way?
Just a minor point... the return value of GlobalVariableSet is indeed a datetime because it is returning the modification time of the variable. It is not setting a datetime.
I hope that makes sense - it can be confusing.
How is it I can display under the Comment what is a Double, into a human readable DateTime?
Set_D1_Bar = GlobalVariableSet("D1_Bar",iTime(NULL,1440,1)); D1_Bar = GlobalVariableGet("D1_Bar"); ...
Resurrecting this thread; slightly different question: how would I go about converting the D1_Bar (Global variable Get()) into a datetime? I am trying to use this in the third parameter of:
int iBarShift( string symbol, // symbol int timeframe, // timeframe datetime time, // time bool exact=false // mode );
(datetime)D1_Bar;
...
Fixed it :)

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
How is it I can display under the Comment what is a Double, into a human readable DateTime?