Page 1 of 1

Time calculate

Posted: 18 Dec 2018 13:48
by boruciak

time_2="{getDate(), dateformat,HH:mm:ss}"

How to calculate time in script?

time=time_2 - time_1


Re: Time calculate

Posted: 18 Dec 2018 15:18
by fagci
d = "1:30:00";

p = split(d, ":");

cDate = getDate();

nDate = addSeconds(cDate,p[2]);
nDate = addMinutes(nDate,p[1]);
nDate = addHours(nDate,p[0]);

fDate ="{nDate, dateformat,HH:mm:ss}";


// sorry, didn't understand question at first read

Re: Time calculate

Posted: 18 Dec 2018 16:45
by Desmanto
Dealing with date in string format, you have to convert it back to a datetime format first. You can use

Code: Select all

time_1 = "07:12:15";
t1 = getDate(time_1, "HH:mm:ss");
But this will give you t1 in 1st January 1970, 07:12:15. Because there is not date, month, year pattern there. So you have to get today's date and append it first to the time_1 and parse it.

Code: Select all

time_1 = "07:12:15";
time_2 = getDate(); //get current time
today = "{time_2,dateformat,dd MMM yyyy}"; // 18 Des 2018
today_time_1 = today + ", " + time_1; // 18 Des 2018, 07:12:15

t1 = getDate(today_time_1, "dd MMM yyyy, HH:mm:ss"); //convert to time
time = abs(time_2 - t1);
I use abs(), as I assume you want to find the difference between time_1 and time_2. It will convert any negative to positive, so give you the difference only.

Re: Time calculate

Posted: 18 Dec 2018 17:25
by fagci
Possible question not correct, because:

If you want to calc time between two dates, simply save them and calc timestamp difference.

If you want substract interval, ex.: 7h, 5min (7:05:00), use my method.

//btw, current day domain is not needeed.

targetTime = '1:30:00';
d1 = getDate(targetTime, "HH:mm:ss");
d2 = getDate("{getDate(),dateformat,HH:mm:ss}", "HH:mm:ss");


Re: Time calculate

Posted: 19 Dec 2018 07:33
by boruciak
Ok thank you for help.
In fact, I did not specify exactly what I mean.
My plan is:
- save getData() to the file
- after some time - reading data from the file
- comparison with current getData
- the result is "something" was done by the "x" time

I still have a question.

//sleep 10m
T="{T,dateformat HH:mm:ss}";

result 01:10:00 - an hour more - why is this?

P.S. Excuse me for asking stupid or obvious questions

Re: Time calculate

Posted: 19 Dec 2018 08:32
by anuraag
boruciak wrote: T="{T,dateformat HH:mm:ss}";
change it to


Re: Time calculate

Posted: 19 Dec 2018 08:44
by boruciak
ok - there was no question.
I get it :)

Re: Time calculate

Posted: 19 Dec 2018 16:48
by Desmanto
@fagci : I also use the similar concept before. But I realized that the time d2 calculated will be in 1st January 1970 range. While in most cases we want to use today's range. So it is better to convert today's range. We can reused the d1 or d2 later if we need them. I usually reused them, so I make it as my best practice to use today's range.

@boruciak : If you are sure that the t2 always later than t1, you don't need abs() anymore. I put that since I thought you are using alarm as the time, which always happen in the future.

If you need to count only the duration, just save the starting point time to a glovar.

Code: Select all

global_starting_point = getDate();
later after finish, subtract it from current time.

Code: Select all

elapsed = getDate() - global_starting_point;
Then you can convert it to duration as pointed out by anuraag.

Time converted without UTC mark, will be affected by your timezone. You are in UTC+1, hence the time will be plus 1 hour.

Re: Time calculate

Posted: 20 Dec 2018 06:46
by boruciak
it's perfect.