# Duration between hours on different days

**URL:** https://community.thunkable.com/t/duration-between-hours-on-different-days/1662003
**Category:** Questions about Thunkable X
**Created:** [December 28, 2021, 11:42pm UTC](https://community.thunkable.com/t/duration-between-hours-on-different-days/1662003 "2021-12-28T23:42:01Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![stronazyskuup](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@stronazyskuup](https://community.thunkable.com/u/stronazyskuup)
#### Post date: [December 28, 2021, 11:42pm UTC](https://community.thunkable.com/t/duration-between-hours-on-different-days/1662003/1 "2021-12-28T23:42:02Z")

</div>

![Zrzut ekranu 29-12-2021 00.18.58](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/7/b/7bac8b09269d2e586d4fb399d973824579862fc3.png)  
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.

---

<div class="post-metadata">

### Author: ![manyone](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@manyone](https://community.thunkable.com/u/manyone)
#### Post date: [December 29, 2021, 11:02pm UTC](https://community.thunkable.com/t/duration-between-hours-on-different-days/1662003/2 "2021-12-29T23:02:06Z")

</div>

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:

```auto
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](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.

---

<div class="post-metadata">

### Author: ![stronazyskuup](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@stronazyskuup](https://community.thunkable.com/u/stronazyskuup)
#### Post date: [December 30, 2021, 10:10am UTC](https://community.thunkable.com/t/duration-between-hours-on-different-days/1662003/3 "2021-12-30T10:10:56Z")

</div>

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

---

<div class="post-metadata">

### Author: ![manyone](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@manyone](https://community.thunkable.com/u/manyone)
#### Post date: [December 30, 2021, 11:12am UTC](https://community.thunkable.com/t/duration-between-hours-on-different-days/1662003/4 "2021-12-30T11:12:14Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![stronazyskuup](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@stronazyskuup](https://community.thunkable.com/u/stronazyskuup)
#### Post date: [December 30, 2021, 11:55am UTC](https://community.thunkable.com/t/duration-between-hours-on-different-days/1662003/5 "2021-12-30T11:55:02Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![jared](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jared/32/138473_2.png) [@jared](https://community.thunkable.com/u/jared)
#### Post date: [December 30, 2021, 12:00pm UTC](https://community.thunkable.com/t/duration-between-hours-on-different-days/1662003/6 "2021-12-30T12:00:05Z")

</div>

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

---

<div class="post-metadata">

### Author: ![muneer](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/muneer/32/75210_2.png) [@muneer](https://community.thunkable.com/u/muneer)
#### Post date: [December 30, 2021, 12:00pm UTC](https://community.thunkable.com/t/duration-between-hours-on-different-days/1662003/7 "2021-12-30T12:00:35Z")

</div>

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).

> [@Date operations (No API)](https://community.thunkable.com/t/date-operations-no-api/1355407):
>
> Hi everyone It´s been a while since my last post in the community I have a couple of questions regarding operations with dates (I didn´t find a solution on the community thad doesn´t use API´s) I need to pick a date and do some maths with it and then show the results in other labels Example: I selected 2021-10-21 Then: Label 1: 2021-10-21 + 5= 2021-10-26 Label 2: 2021-10-26 (result of the previous operation) + 10= 2021-11-05 Of course ther is two “problems”. Wich blocks I need to use? Ho…

---

<div class="post-metadata">

### Author: ![stronazyskuup](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@stronazyskuup](https://community.thunkable.com/u/stronazyskuup)
#### Post date: [December 30, 2021, 2:22pm UTC](https://community.thunkable.com/t/duration-between-hours-on-different-days/1662003/8 "2021-12-30T14:22:46Z")

</div>

![Zrzut ekranu 30-12-2021 15.18.42](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/a/6/a62371579afb0f0102b13733e3f25f0b5244972a.png)  
i did something like this and it works for one day difference and i don’t need more.

---

<div class="post-metadata">

### Author: ![manyone](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@manyone](https://community.thunkable.com/u/manyone)
#### Post date: [December 30, 2021, 5:20pm UTC](https://community.thunkable.com/t/duration-between-hours-on-different-days/1662003/9 "2021-12-30T17:20:47Z")

</div>

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

---

<div class="post-metadata">

### Author: ![muneer](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/muneer/32/75210_2.png) [@muneer](https://community.thunkable.com/u/muneer)
#### Post date: [December 30, 2021, 6:45pm UTC](https://community.thunkable.com/t/duration-between-hours-on-different-days/1662003/10 "2021-12-30T18:45:00Z")

</div>

Using JavaScript in a `Web Viewer`, you will do this

```auto
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.

---

<div class="post-metadata">

### Author: ![manyone](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@manyone](https://community.thunkable.com/u/manyone)
#### Post date: [December 30, 2021, 7:21pm UTC](https://community.thunkable.com/t/duration-between-hours-on-different-days/1662003/11 "2021-12-30T19:21:59Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![stronazyskuup](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@stronazyskuup](https://community.thunkable.com/u/stronazyskuup)
#### Post date: [December 31, 2021, 10:47am UTC](https://community.thunkable.com/t/duration-between-hours-on-different-days/1662003/12 "2021-12-31T10:47:19Z")

</div>

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

---

<div class="post-metadata">

### Author: ![stronazyskuup](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@stronazyskuup](https://community.thunkable.com/u/stronazyskuup)
#### Post date: [December 31, 2021, 10:49am UTC](https://community.thunkable.com/t/duration-between-hours-on-different-days/1662003/13 "2021-12-31T10:49:22Z")

</div>

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

---

<div class="post-metadata">

### Author: ![muneer](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/muneer/32/75210_2.png) [@muneer](https://community.thunkable.com/u/muneer)
#### Post date: [December 31, 2021, 1:01pm UTC](https://community.thunkable.com/t/duration-between-hours-on-different-days/1662003/14 "2021-12-31T13:01:19Z")

</div>

See this from my side  
[https://x.thunkable.com/projectPage/61cee4c82390290010915968](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

```auto
<!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

---

<div class="post-metadata">

### Author: ![jared](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jared/32/138473_2.png) [@jared](https://community.thunkable.com/u/jared)
#### Post date: [December 31, 2021, 3:14pm UTC](https://community.thunkable.com/t/duration-between-hours-on-different-days/1662003/15 "2021-12-31T15:14:36Z")

</div>

> [@muneer](#):
>
> See this from my side  
> [Thunkable](https://x.thunkable.com/projectPage/61cee4c82390290010915968)

Nice share!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/0/f/0f59f292712368bce16ff80133ae10de8a6f27e8.png) [@system](https://community.thunkable.com/u/system)
#### Post date: [March 31, 2022, 3:14pm UTC](https://community.thunkable.com/t/duration-between-hours-on-different-days/1662003/16 "2022-03-31T15:14:51Z")

</div>

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