Calculate elapsed time inside QML
-
Hi,
I need calculate elapsed time form readed timestamp.
In QML I do like below:var readed = new Date(timestamReaded * 1000) var actual = new Date() var elapsed = new Date(actual - readed) function roundToHour(date){ var p = 60*60*1000; return new Date(Math.round(date.getTime() /p)*p) } var elapsedAround = roundToHour(elapsed); mytextfield.text = elapsedAround.getHours()It works, but only when reded timestamp and actual date is in the same day.... What I should change to calculate hours when readed timestamp is from previus day ?
-
@piervalli I am still don't understand ....
@Damian7546
If you need to calculate diff date, you can use a simple js algoritme.
You can use a simple js datefunction inDays (d1, d2) { var t2 = d2.getTime(); var t1 = d1.getTime(); return Math.floor((t2-t1)/(24*3600*1000)); } function inHours (d1, d2) { var t2 = d2.getTime(); var t1 = d1.getTime(); return Math.floor((t2-t1)/(3600*1000)); } Component.onCompleted:{ var d1 = new Date() var d2 = new Date(2023,3,1) console.error("inDays",inDays(d1,d2)) }Olther examples are in the link of stackoverflow, for hours
Instanded console.time and console.timeEnd is used to calculated diff time in a function (benchmark)
https://stackoverflow.com/questions/7763327/how-to-calculate-date-difference-in-javascript
-
Hi,
I need calculate elapsed time form readed timestamp.
In QML I do like below:var readed = new Date(timestamReaded * 1000) var actual = new Date() var elapsed = new Date(actual - readed) function roundToHour(date){ var p = 60*60*1000; return new Date(Math.round(date.getTime() /p)*p) } var elapsedAround = roundToHour(elapsed); mytextfield.text = elapsedAround.getHours()It works, but only when reded timestamp and actual date is in the same day.... What I should change to calculate hours when readed timestamp is from previus day ?
You can use use.
console.time("wholeFunction");
console.timeEnd("wholeFunction"); -
You can use use.
console.time("wholeFunction");
console.timeEnd("wholeFunction");This post is deleted! -
You can use use.
console.time("wholeFunction");
console.timeEnd("wholeFunction");@piervalli I am still don't understand ....
-
@piervalli I am still don't understand ....
@Damian7546
If you need to calculate diff date, you can use a simple js algoritme.
You can use a simple js datefunction inDays (d1, d2) { var t2 = d2.getTime(); var t1 = d1.getTime(); return Math.floor((t2-t1)/(24*3600*1000)); } function inHours (d1, d2) { var t2 = d2.getTime(); var t1 = d1.getTime(); return Math.floor((t2-t1)/(3600*1000)); } Component.onCompleted:{ var d1 = new Date() var d2 = new Date(2023,3,1) console.error("inDays",inDays(d1,d2)) }Olther examples are in the link of stackoverflow, for hours
Instanded console.time and console.timeEnd is used to calculated diff time in a function (benchmark)
https://stackoverflow.com/questions/7763327/how-to-calculate-date-difference-in-javascript
-
D Damian7546 has marked this topic as solved on