Double to DateTime

 
...

Set_D1_Bar = GlobalVariableSet("D1_Bar",iTime(NULL,1440,1));
D1_Bar     = GlobalVariableGet("D1_Bar");
D1_Bias    = "Bearish";

Comment("D1 Bias is: "+D1_Bias+" since: "+DoubleToStr(D1_Bar,TIME_DATE|TIME_MINUTES),
        "\nH4 Bias is: "+H4_Bias,
        "\nH1 Bias is: ",H1_Bias);

...

How is it I can display under the Comment what is a Double, into a human readable DateTime?
 

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"

 
winterz:

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

 
gooly: date you save could be 'small' enough to get back the same date+time.
A double has 15-17 digits 1417305600 (Sun, 30 Nov 2014 00:00:00 GMT) has 10. No problem going from datatime -> double -> datetime. Going to an int fails on 2147483647 (Tue, 19 Jan 2038 03:14:07 GMT)
 
winterz:

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. 

 
Thank you kindly for everyones input. It's been very helpful!
 
winterz:

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 :)
Reason: