Time Confused!!

 

Please can someone show me how to calculate the number of seconds between 2 time points.

i.e the difference in seconds between Bar[0] and Bar[10]?

The datetime type is intended for storing the date and time as the number of seconds elapsed since January 01, 1970. This type occupies 8 bytes of memory. This is fine but how do you display Time[0] in seconds as opposed to datetime format? So Print(Time[0]) gives 2014.10.15 04:00:00?

 The DateTime function is always baffling!!

thanks for any replies. 

 

 

Just subtract the 2 times from one another:

printf("The time difference in seconds between Bar 0 and Bar 10 is %i", Time[0]-Time[10]);

 

You might want to take a look at type casting and also how Print works "Dates are shown as YYYY.MM.DD HH:MI:SS. To show data in another format, use TimeToString(). "

 
Thanks. I've just come over from MT4 and haven't used printf before. It seems it was this that was confusing because as the documentation says the datetime(s) are stored as seconds.
 
sd59: how do you display Time[0] in seconds as opposed to datetime format?

 

  Print((int)Time[0]);
 

If there is only one Print argument, and it is a date, it will use the default display of YYYY.MM.DD HH:MI:SS.... the value is stored as seconds since the start of 1970 however.

Print(Time[0]);

 

 

If the only argument is a calculated date, it still gives the difference in seconds converted back to a date again... so 10 minutes after 01 Jan 1970.

Print(Time[0]-Time[10]);

 

 

If you mix arguments in a Print statement, and not all of them are strings, you will now get a warning but again the difference in seconds will be printed as a date (the default handling of a datetime):

Print("The time difference in seconds between Bar 0 and Bar 10 is " + (Time[0]-Time[10]));

 

 

 

To get rid of the warning and give the "expected" result, you can use:

Print("The time difference in seconds between Bar 0 and Bar 10 is " + IntegerToString(Time[0]-Time[10]));

 

 

Which is the same as: 

printf("The time difference in seconds between Bar 0 and Bar 10 is %i", Time[0]-Time[10]);

 

printf is just an abbreviated way to write PrintFormat

Reason: