# Calculate time in hours

**URL:** <https://community.homey.app/t/calculate-time-in-hours/108326>\
**Category:** Questions & Help\
**Created:** [April 16, 2024, 7:51pm UTC](https://community.homey.app/t/calculate-time-in-hours/108326 "2024-04-16T19:51:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![harmenkuijer](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/harmenkuijer/32/120683_2.png) [@harmenkuijer](https://community.homey.app/u/harmenkuijer)\
**Post date:** [April 16, 2024, 7:51pm UTC](https://community.homey.app/t/calculate-time-in-hours/108326/1 "2024-04-16T19:51:45Z")

</div>

Hi there!

I have searched the forum many, many times and tried several apps but I can’t find what I am looking for. Looks quite simple but unfortunatly.

I’m looking a method to calculate with hours. What I like to do:

timenow (time card) + 2 hours = variable (eindtijd)

Staring blind at it ☹

Anyone…

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [April 17, 2024, 2:33am UTC](https://community.homey.app/t/calculate-time-in-hours/108326/2 "2024-04-17T02:33:38Z")

</div>

Hi Harmen,

> [@\[APP\]\[Pro\] Better Logic Library - For Users](https://community.homey.app/t/app-pro-better-logic-library-for-users/71876/144):
>
> found it! This calculates the difference between the two times in minutes Note that the logic cards needs to be set to text, not variable!

---

<div class="post-metadata">

**Author:** ![harmenkuijer](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/harmenkuijer/32/120683_2.png) [@harmenkuijer](https://community.homey.app/u/harmenkuijer)\
**Post date:** [April 20, 2024, 12:03pm UTC](https://community.homey.app/t/calculate-time-in-hours/108326/3 "2024-04-20T12:03:46Z")

</div>

@Peter_Kawa, thanks! Ik zie het alleen nog ff niet, maar dat kan aan mij liggen ☹

Misschien handig voor de context:

- Ik heb een systeem waarbij ik mij aan/af moet melden. Dit wil ik automatiseren aan de hand van een trigger. Ik moet echter opgeven: starttijd + startdatum en eindtijd + einddatum. Nu wil ik me standaar bijvoorbeeld 24 uur afmelden. Ik kan startijd en eindtijd (variabele tijd) en startdatum (variabele datum) wel ophalen. Maar ik moet ook de datum van morgen hebben, dit lukt me echter nog niet goed.

Via HomeyScript voer ik onderstaand script uit (om 00:05), maar blijft om 1 of andere gekke reden de dag van “vandaag” teruggeven…

```auto
// Datum van vandaag
let today = new Date();

// Morgen uitrekenen door dag er bij op te tellen
let tomorrow = new Date(today);
tomorrow.setDate(today.getDate() + 1);

// Datum omzetten
let dd = String(tomorrow.getDate()).padStart(2, '0');
let mm = String(tomorrow.getMonth() + 1).padStart(2, '0'); // Januari is 0!
let yyyy = tomorrow.getFullYear();

let tomorrowFormatted = dd + '-' + mm + '-' + yyyy;

// Console log
console.log('Morgen is het ' + tomorrowFormatted);

// Aanmaken en updaten variabele #DatumMorgen
await tag('DatumMorgen', tomorrowFormatted);

```

Daarnaast wil ik ook graag kunnen rekenen met bijvoorbeeld “tijd nu” + variabele (1, 2, 4, 6, 8, 12, etc uur)

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [April 20, 2024, 1:29pm UTC](https://community.homey.app/t/calculate-time-in-hours/108326/4 "2024-04-20T13:29:25Z")

</div>

> [@harmenkuijer](#):
>
> (at 00:05), but for some crazy reason keeps returning the day of “today”…

Homey works internally with UTC time; NL daylight saving time is +2  
So you are now starting the script at 22:05 according to Homey’s internal time view.

Try starting that flow at 02:01, that should solve it.

.

> [@harmenkuijer](#):
>
> In addition, I would also like to be able to calculate with, for example, “time now” + variable (1, 2, 4, 6, 8, 12, etc hours)

So do you mean that variable contains the number 1, 2, 4 etc.?

---

<div class="post-metadata">

**Author:** ![harmenkuijer](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/harmenkuijer/32/120683_2.png) [@harmenkuijer](https://community.homey.app/u/harmenkuijer)\
**Post date:** [April 20, 2024, 1:32pm UTC](https://community.homey.app/t/calculate-time-in-hours/108326/5 "2024-04-20T13:32:17Z")

</div>

> [@Peter\_Kawa](#):
>
> Homey werkt intern met UTC tijd; NL zomertijd is +2  
> Je start het script dus om 22:05 vanuit Homey’s interne tijd bekeken.
> 
> Probeer eens die flow om 02:01 te starten, dat moet de oplossing zijn.

Ohh. Logisch ook wel, daar was ik niet opgekomen.

> Bedoel je dan dat die variabele het cijfer 1, 2, 4 etc. bevat?

Ik wil graag die uren kunnen aanpassen idd of meerdere variaties kunnen gebruiken 🙂

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [April 20, 2024, 1:36pm UTC](https://community.homey.app/t/calculate-time-in-hours/108326/6 "2024-04-20T13:36:50Z")

</div>

Maybe you can use something out of this:

```auto
// get_tomorrow+month.js
// this script returns the tomorrow's numeric date (day + month)  

// USER INPUT
// Enter the month names used in Homey's language
var month01 = "January";
var month02 = "February";
var month03 = "March";
var month04 = "April";
var month05 = "May";
var month06 = "June";
var month07 = "July";
var month08 = "August";
var month09 = "September";
var month10 = "October";
var month11 = "November";
var month12 = "December";
// END USER INPUT

let SysInfo = await Homey.system.getInfo();
let UTC = new Date();

// show only the last "word" of the date human string,
// in order to return the current time
var localtime = SysInfo.dateHuman.split(" ")[4];
var month = SysInfo.dateHuman.split(" ")[2];
//var utcMonthNoTomorrow = UTC.getMonth()+1;
//var utcDayNoTomorrow = UTC.getDay()+0;

let utcDate = UTC.toDateString();
// Isolate the day number from the string
let utcDayNo = utcDate.slice(8,10);
let monthNo = UTC.toLocaleDateString().split("/")[0];

  // if day number has a trailing 0, remove it
  if ( utcDayNo.slice(0,1) == 0 ) {
    //console.log("check: does day number have a leading zero ('0' is OK): ",utcDayNo.slice(0,1));
    utcDayNo = utcDayNo.slice(1);
    //console.log("check: day number without leading zero = ",utcDayNo);
  }

// Add 1 for tomorrow's date
let tomorrow = Number(utcDayNo) + 1 ;

  // if "tomorrow" is higher than a month's highest day number
  if ( tomorrow == 29 ) {
    if ( month == month02 ) {
       tomorrow = 1
       monthNo = 03
       }
  };     
  if ( tomorrow == 31) {
    if ( month == month04 ) {
       tomorrow = 1
       monthNo = 05
       } 
      if ( month == month06 ) {
         tomorrow = 1
         monthNo = 07
         }
        if ( month == month09 ) {
           tomorrow = 1
           monthNo = 10
           }
          if ( month == month11 ) {
             tomorrow = 1
             monthNo = 12
             }    
  };
    
  if ( tomorrow == 32 ) {   
    if ( month == month01 ) {
       tomorrow = 1
       monthNo = 02
       }
      if ( month == month03 ) {
         tomorrow = 1
         monthNo = 05
         } 
        if ( month == month05 ) {
           tomorrow = 1
           monthNo = 06
           }
          if ( month == month07 ) {
             tomorrow = 1
             monthNo = 08
             }
              if ( month == month08 ) {
                 tomorrow = 1
                 monthNo = 09
                 }
                if ( month == month10 ) {
                   tomorrow = 1
                   monthNo = 11
                   }
                  if ( month == month12 ) {
                     tomorrow = 1
                     monthNo = 01
                     }
  };
  // If day number is below 10, add a leading zero
  if ( tomorrow < 10 ){
    tomorrow = "0"+tomorrow
  };

// Return the local time
// console.log("localtime: ",localtime);
// Enable if you like (for checking f.i.)
// console.log("Local date =", SysInfo.dateHuman);
// console.log("Month =",month);
console.log("Month number =",monthNo);
// console.log("UTC timestamp =", UTC);
// console.log("var utcDate =",utcDate);
console.log("Today number =",utcDayNo);
// console.log("var utcDayNoTomorrow =",utcDayNoTomorrow); 
// console.log("var utcMonthNoTomorrow =",utcMonthNoTomorrow,"\n\n"); 
console.log("Tomorrow number =",tomorrow);

return(monthNo+"-"+tomorrow);

```
