Page 1 of 1

Date time addition and subraction

Posted: 04 Dec 2016 19:02
by whenevere
Unless I miss something very simple, I can't seem to find functions to do addition and subtraction on datetime ...

How do I get a time difference?

global_var1="{getDate(),dateformat,HH:mm:ss}";
global_var2="{getDate(),dateformat,HH:mm:ss}";
timedifference="{global_var2-global_var1,dateformat,HH:mm:ss}";

The above script does not work.

For example, I have a datetime1 of "13:22:10" and a datetime2 of "13:23:30". I want to do the subtraction between datetime2 and datetime1, and I expect to get the result of "1m 20s".

I read somewhere and something about converting datetimes to milliseconds since 1970, doing subtraction on milliseconds, and then convert the milliseconds back to datetime format ... is this the way to do it? if so, how? How to do addition and subtraction on datetimes and get the result back in {datetime, pattern} format or duration string format??

Re: Date time addition and subraction

Posted: 04 Dec 2016 23:05
by P-chu
Hi whenevere,

First, you got quotes around your brackets {}, so your variables will all contain strings (text) values, not numeric.
Also, I could be wrong, but I think the "dateformat" is used only when you are going to output the value (often as a text) to something like a file, or message, or speech. So maybe your script should be more simpler:
global_var1 = getDate();
global_var2 = getDate();
timedifference = global_var2 - global_var1;
Then when your flow reports back the difference, you could use the "dateformat" in your message or notification. (if Martin or MURTUMA can verify; is that date type value in seconds?)

Hope that offers some help.

Re: Date time addition and subraction

Posted: 05 Dec 2016 16:42
by Scotty
To reply to P-chu's question: getDate returns the time in milliseconds (not seconds). The value returned shows the number of milliseconds that have elapsed since January 1, 1970, 00:00:00 GMT.

Therefore the calculated variable (timedifference) also is reported in milliseconds.

BTW, @whenevere: for future reference: there's a good start guide to scripts (including the date, time & time difference) here. You asked not only how to calculate the time difference (which P-Chu has explained, naming the result timedifference) but also how to format that result into "recognizable" time. There are at least 2 ways to do this:

1. value = "{timedifference,dateformat,timezone,UTC,HH:mm:ss}"; // the term value is a text string that shows the timedifference in hours, minutes & seconds (e.g. 01:22:34). This works only for values < 24 hours.

2. value = getDurationString(timedifference); // in this case, value would be a text string that is auto-formatted to days, hours, minutes, seconds, milliseconds. So, for example, 1h 22m 34s 123ms. SFAIK this works for any timedifference, but for very large values, it doesn't format to the level of years (e.g., it'll say 400d, rather than 1y 35d). Not sure why, maybe it doesn't want to assume 365 days in a year (because some have 366). Finally, if milliseconds is more detail than you want, you can remove the mSec value by interrogating the string value and capturing the substring within value that includes only the info you want (e.g., 1h 22m 34s).