Duration between hours on different days


This is how I calculate the duration. it works, but there is a problem when the hours are not on the same day. Is there a way to do this? i found one forum post but the calculations there are in google sheet.

2 Likes

you will need to include the full year month day into your calculation. say you’ve got 2 date time values, date1, hour1, minute1 and date2, hour2, minute2 - and you want to compute the difference (in minutes) between the two. simply convert date1 to serialdate1 (number of days since march 1960), do the same for the 2nd date, then compute the following:

serialtime1 = (serialdate1 * 24 * 60) + (hour1 * 60) + minute1
serialtime2 = (serialdate2 * 24 * 60) + (hour2 * 60) + minute2

now both values are expressed as number of minutes since march 1, 1960. simply subtract the 2 values and you get the correct duration between the two values - in minutes!

how do you convert a date to serial date?
link to my project:
https://x.thunkable.com/copy/cb9d53a5dee666da14d5f0a1a334f19d

cut-n-paste the block ymd2serial and use it to compute date serial from year,moth, day. then convert the values to minutes and it’s just a matter of subtraction to compute duration.

3 Likes

Okay. How to make one date and time? how to combine it? is “serial date” the only way?

1 Like

you can use @muneer’s trick: create a google sheet with 5 columns: date1, time1, date2, time2, diff and enter the formula in diff as the difference between the 2 date and times. update the first 4 columns then re-read the whole row and get the value of diff.

2 Likes

in kodular or app inventor it is built-in. Here really is no other way here than linking to google sheets just to calculate the duration?

1 Like

To get a duration of time between two points of time, your best bet is to use the seconds since 1970 block.

End time - start time / 60 = duration in minutes

You can use a web viewer and get the duration using JavaScript code.
You can also build a Google Apps Script code and deploy it so you can use it as web API.

Thunkable X is open code in this regards so you can choose many other ways to get the calculations.

See this post which I explained an example of how to get the date after number of days (added or subtracted).

1 Like


i did something like this and it works for one day difference and i don’t need more.

How would you handle th e difference between 12/31/2021 11:30am to 1/1/2022 12:30pm.?

1 Like

Using JavaScript in a Web Viewer, you will do this

const startDate  = '2021-12-31';
const endDate    = '2022-01-01';

const diffInMs   = new Date(endDate) - new Date(startDate)
const diffInDays = diffInMs / (1000 * 60 * 60 * 24);

console.log( diffInDays  );

This will work for any two dates.

2 Likes

My question was meant for @stronazyskuup -
I just wanted to illustrate that he needs dates in order to do tiime differences properly. However if his app will never encounter durations over 24 hours, his method will do nicely.

1 Like

yes, I emphasized the fact that it works up to one day :slight_smile:

when I find free time I will check this method. I can see that it is better.

1 Like

See this from my side
https://x.thunkable.com/projectPage/61cee4c82390290010915968

This project will send your input to a JavaScript code in a Web Viewer and will get back the number of seconds between the two dates.

The rest is done in the project.

This is the JavaScript Code

<!DOCTYPE html>
<html>
<head>
<script 
    src="https://thunkable.github.io/webviewer-extension/thunkableWebviewerExtension.js"
    type="text/javascript"></script>
</head>

<script>
  ThunkableWebviewerExtension.receiveMessage(function(message) {
    const twoDates = JSON.parse(message);
    ThunkableWebviewerExtension.postMessage((new Date(twoDates.endDate) - new Date(twoDates.startDate))/1000);
});
</script>
</html>

You can of course enhance on it but this should give you a good start.

CC:
@manyone
@jared
@tatiang

4 Likes

Nice share!

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.