Time calculate
Moderator: Martin
Time calculate
Hi
time_1=07:12:15
time_2="{getDate(), dateformat,HH:mm:ss}"
How to calculate time in script?
time=time_2 - time_1
Thx
time_1=07:12:15
time_2="{getDate(), dateformat,HH:mm:ss}"
How to calculate time in script?
time=time_2 - time_1
Thx
Re: Time calculate
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}";
log(fDate);
// sorry, didn't understand question at first read
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}";
log(fDate);
// sorry, didn't understand question at first read
Last edited by fagci on 18 Dec 2018 16:56, edited 2 times in total.
All systems nominal.
Re: Time calculate
Dealing with date in string format, you have to convert it back to a datetime format first. You can use
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.
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.
Code: Select all
time_1 = "07:12:15";
t1 = getDate(time_1, "HH:mm:ss");
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);
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.
Re: Time calculate
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");
log(1.0*(d2-d1)/1000/60/60);
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");
log(1.0*(d2-d1)/1000/60/60);
All systems nominal.
Re: Time calculate
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.
t1=getDate();
//sleep 10m
t1=getDate();
T=abs(t2-t1);
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
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.
t1=getDate();
//sleep 10m
t1=getDate();
T=abs(t2-t1);
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
change it toboruciak wrote: T="{T,dateformat HH:mm:ss}";
T=getDurationString(T);
Or
T="{T,dateformat,timezone,UTC,HH:mm:ss}"
Re: Time calculate
ok - there was no question.
I get it
Tnx
I get it
Tnx
Re: Time calculate
@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.
later after finish, subtract it from current time.
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.
@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();
Code: Select all
elapsed = getDate() - global_starting_point;
Time converted without UTC mark, will be affected by your timezone. You are in UTC+1, hence the time will be plus 1 hour.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.