# Calculate number of hours/minutes between "now" and a future point in time

**URL:** <https://community.homey.app/t/calculate-number-of-hours-minutes-between-now-and-a-future-point-in-time/75617>\
**Category:** Flows\
**Created:** [January 26, 2023, 5:59pm UTC](https://community.homey.app/t/calculate-number-of-hours-minutes-between-now-and-a-future-point-in-time/75617 "2023-01-26T17:59:46Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jux2k2](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jux2k2/32/68336_2.png) [@Jux2k2](https://community.homey.app/u/Jux2k2)\
**Post date:** [January 26, 2023, 5:59pm UTC](https://community.homey.app/t/calculate-number-of-hours-minutes-between-now-and-a-future-point-in-time/75617/1 "2023-01-26T17:59:46Z")

</div>

I would like to calculate dynamically the # hours or minutes between the moment a given flow is started and a known future time.

For example target time is 20.00 and the flow is started at 13.40, number of hours is 6 or 7 depending on the rounding etc.

I cant find any means of doing so. Any app that could help? Or am I missong something? Thx

---

<div class="post-metadata">

**Author:** ![spkesDE](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/spkesde/32/54352_2.png) [@spkesDE](https://community.homey.app/u/spkesDE)\
**Post date:** [January 26, 2023, 6:23pm UTC](https://community.homey.app/t/calculate-number-of-hours-minutes-between-now-and-a-future-point-in-time/75617/2 "2023-01-26T18:23:13Z")

</div>

Check out BLL from fellow developer @Arie_J_Godschalk

> [@\[APP\]\[Pro\] Better Logic Library - For Users](https://community.homey.app/t/app-pro-better-logic-library-for-users/71876):
>
> BLL is Live! If you are an App Developer and want to know more about how to include this into your App or how to let your App support BLL coding, goto the [[APP][Pro] Better Logic Library - For Developers](https://community.homey.app/t/app-pro-better-logic-library-for-developers/71761) Forum Topic. Part of: [Better Logic Library - Just some better logic, variable and library management](https://community.homey.app/t/app-pro-better-logic-just-some-better-logic-and-variable-management/5568)Homey’s first real Library: Better Logic Library! The [Better Logic](https://homey.app/a/net.i-dev.betterlogic/) App has a new addition: A library and library management for other apps to consume. This Topic describes how users can use th…

---

<div class="post-metadata">

**Author:** ![martijnpoppen](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/martijnpoppen/32/84849_2.png) [@martijnpoppen](https://community.homey.app/u/martijnpoppen)\
**Post date:** [January 27, 2023, 8:23am UTC](https://community.homey.app/t/calculate-number-of-hours-minutes-between-now-and-a-future-point-in-time/75617/3 "2023-01-27T08:23:07Z")

</div>

Or check out [Flow Utilities App for Homey | Homey](https://homey.app/en-us/app/com.flow.utilities/Flow-Utilities/)

> [@\[APP\]\[Pro\] Flow Utilities](https://community.homey.app/t/app-pro-flow-utilities/53027/6):
>
> Some Flow ideas to use this App with: Notification: [Screenshot 2021-10-12 at 12.48.29]

---

<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:** [January 28, 2023, 2:04am UTC](https://community.homey.app/t/calculate-number-of-hours-minutes-between-now-and-a-future-point-in-time/75617/4 "2023-01-28T02:04:38Z")

</div>

Also interesting maybe:

> [@\[APP\]\[Pro\] Action Scheduler](https://community.homey.app/t/app-pro-alarm-utilities/75592#setting-a-dynamic-alarm-time-with-weekdays-since-v100-9):
>
> Contribute If you enjoy what I do, consider supporting me on [Ko-fi](https://ko-fi.com/oh2th) or [Paypal](https://paypal.com/oh2th)! Every little bit means the world! [[ko-fi]](https://ko-fi.com/oh2th)[[paypal-donate]](https://paypal.me/oh2th) Introduction Dynamic flow scheduling with a Scheduler device configurable from flows. App Featuresplay_button Flow cards: And - Conditions Device Features Device: Cron Timeplay_button Adding a new deviceplay_button Capabilitiesplay_button Flow cards: When - Triggersplay_button Flow cards: And - Conditionsplay_button Flow cards: Then - Actions Device: Schedulerplay_button Adding a new …

---

<div class="post-metadata">

**Author:** ![OH2TH](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/oh2th/32/111414_2.png) [@OH2TH](https://community.homey.app/u/OH2TH)\
**Post date:** [January 28, 2023, 7:59am UTC](https://community.homey.app/t/calculate-number-of-hours-minutes-between-now-and-a-future-point-in-time/75617/5 "2023-01-28T07:59:35Z")

</div>

Hello @Jux2k2,

Did I understand you question correctly? Atleast one of these three:

1. You want to know how much time is on between known start and end times? The the answer could be Better Logic Library to do math between the too times.

2. Or you only know the end time / target time for something to be ready and want to start a flow X minutes before that target time, then the answer is Alarm Utilities as @Peter_Kawa pointed out. However only the [test version](https://homey.app/a/fi.oh2th.alarmutils/test) currently has the flow card to do the math for you.I expect to get that version published early next week.

3. And finally the Flow Utilities app can do similar things like calculate the elapsed time from a known start time.

---

<div class="post-metadata">

**Author:** ![Jux2k2](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jux2k2/32/68336_2.png) [@Jux2k2](https://community.homey.app/u/Jux2k2)\
**Post date:** [January 28, 2023, 5:11pm UTC](https://community.homey.app/t/calculate-number-of-hours-minutes-between-now-and-a-future-point-in-time/75617/6 "2023-01-28T17:11:06Z")

</div>

Hei

Thanks for the help so far.

To your question:  
-I do NOT know the start time, it has to be “now”, when the flow is started

-Flow is started by an automatic trigger, so I do not manually control it

-I DO know the end time, an hour (integer).

-My objective is to calculate the period (in hours, integer variable, it will require rounding up or down) so I can use it in other flows.

-Given that the end time is just an hour without date, the duration is below 24hrs 🙂

---

<div class="post-metadata">

**Author:** ![Jux2k2](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jux2k2/32/68336_2.png) [@Jux2k2](https://community.homey.app/u/Jux2k2)\
**Post date:** [January 28, 2023, 5:12pm UTC](https://community.homey.app/t/calculate-number-of-hours-minutes-between-now-and-a-future-point-in-time/75617/7 "2023-01-28T17:12:21Z")

</div>

I guess what I cant find is a card that allows me to identify what “now” is and extract the hour from it…

---

<div class="post-metadata">

**Author:** ![OH2TH](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/oh2th/32/111414_2.png) [@OH2TH](https://community.homey.app/u/OH2TH)\
**Post date:** [January 28, 2023, 5:15pm UTC](https://community.homey.app/t/calculate-number-of-hours-minutes-between-now-and-a-future-point-in-time/75617/8 "2023-01-28T17:15:32Z")

</div>

Better Logic Library can extract the current hour for you.

I’m trying to understand what is you end goal? What do you want to achieve as a result?

---

<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:** [January 28, 2023, 6:56pm UTC](https://community.homey.app/t/calculate-number-of-hours-minutes-between-now-and-a-future-point-in-time/75617/9 "2023-01-28T18:56:01Z")

</div>

here’s a BLL example

> [@\[APP\]\[Pro\] Better Logic Library - For Users](https://community.homey.app/t/app-pro-better-logic-library-for-users/71876/28):
>
> Well, you can kind off, but not in the easy ‘formatting’ way: because i didn’t find a all-around solutions independend of locale and timezone. date('DATETIME', null, 'en') You will get a tag, you can use the last number if it’s like this. But if you get this, you will probably get the same result as me: So, the complete solution would be: new Date().setHours(18-Number.parseInt(\_.last(date('DATETIME', null, 'en'))),0,0,0) - Date.now() \< 15\*60\*1000; (this is true when less the…

---

<div class="post-metadata">

**Author:** ![Jux2k2](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jux2k2/32/68336_2.png) [@Jux2k2](https://community.homey.app/u/Jux2k2)\
**Post date:** [January 28, 2023, 7:52pm UTC](https://community.homey.app/t/calculate-number-of-hours-minutes-between-now-and-a-future-point-in-time/75617/10 "2023-01-28T19:52:34Z")

</div>

Thanks guys, will look into this!

Im setting up a smart EV charging plan with this flow.  
From the moment I plug the car I dont care when it charges as long as its charged by 9 am.  
I can calculate how many hours of charge are required but still need to find that # of cheapest hours within the period from “now” and 9 am.

Also, I am running a peak shaving program so the charging speed may be drastically reduced to allow for heating to run. Each time the charging current is changed I need to update the charging plan (hence update the period duration).

I hope it makes sense 🙂

---

<div class="post-metadata">

**Author:** ![Hamster](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/hamster/32/16580_2.png) [@Hamster](https://community.homey.app/u/Hamster)\
**Post date:** [January 31, 2023, 8:52am UTC](https://community.homey.app/t/calculate-number-of-hours-minutes-between-now-and-a-future-point-in-time/75617/11 "2023-01-31T08:52:05Z")

</div>

Here’s another way, a HomeyScript which returns the number of minutes to a target time:

```auto
let now = new Date();

let now_local = now.toLocaleTimeString("nl-NL",
   {
  hour12: false,
  timeZone: "Europe/Amsterdam",
  hour: "2-digit",
  minute: "2-digit",
  });

let now_hours = Number(now_local.slice(0,2));
let now_minutes = Number(now_local.slice(3,5));

let target = args[0]; //For example "20:45";
let target_hours = Number(target.slice(0,2));
let target_minutes = Number(target.slice(3,5));

if (target_hours==now_hours && target_minutes==now_minutes){
  //Target time is equal to now, so return 0
  console.log("Target time is now!");
  return 0;
}else{
  if ((target_hours>now_hours) || (target_hours==now_hours && target_minutes>=now_minutes)){
    console.log("Target time is today!");
    return (target_minutes+60-now_minutes)+(target_hours-1-now_hours)*60
  }else{
    console.log("Target time is tomorrow!");
    return (target_minutes+60-now_minutes)+(target_hours+23-now_hours)*60
  }
}

```

---

<div class="post-metadata">

**Author:** ![Hamster](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/hamster/32/16580_2.png) [@Hamster](https://community.homey.app/u/Hamster)\
**Post date:** [February 8, 2023, 1:49pm UTC](https://community.homey.app/t/calculate-number-of-hours-minutes-between-now-and-a-future-point-in-time/75617/12 "2023-02-08T13:49:24Z")

</div>

@Jux2k2, did you find a solution?

---

<div class="post-metadata">

**Author:** ![Jux2k2](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jux2k2/32/68336_2.png) [@Jux2k2](https://community.homey.app/u/Jux2k2)\
**Post date:** [February 9, 2023, 8:34pm UTC](https://community.homey.app/t/calculate-number-of-hours-minutes-between-now-and-a-future-point-in-time/75617/13 "2023-02-09T20:34:47Z")

</div>

Between work and toddlers I havent had time yet. Will keep the group updated!

---

<div class="post-metadata">

**Author:** ![Patrick\_Van\_Deursen](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/patrick_van_deursen/32/56539_2.png) [@Patrick\_Van\_Deursen](https://community.homey.app/u/Patrick_Van_Deursen)\
**Post date:** [March 31, 2024, 6:22pm UTC](https://community.homey.app/t/calculate-number-of-hours-minutes-between-now-and-a-future-point-in-time/75617/14 "2024-03-31T18:22:27Z")

</div>

calculated the time between two variable moments. you can replace one of the with “now”

> [@\[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

---

<div class="post-metadata">

**Author:** ![Rrrr](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/rrrr/32/629_2.png) [@Rrrr](https://community.homey.app/u/Rrrr)\
**Post date:** [April 7, 2024, 9:47am UTC](https://community.homey.app/t/calculate-number-of-hours-minutes-between-now-and-a-future-point-in-time/75617/15 "2024-04-07T09:47:16Z")

</div>

> [@Jux2k2](#):
>
> still need to find that # of cheapest hours within the period from “now” and 9 am

Did you check out PBTH?

---

<div class="post-metadata">

**Author:** ![PjotrAmslap](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/pjotramslap/32/79935_2.png) [@PjotrAmslap](https://community.homey.app/u/PjotrAmslap)\
**Post date:** [June 25, 2024, 2:26pm UTC](https://community.homey.app/t/calculate-number-of-hours-minutes-between-now-and-a-future-point-in-time/75617/16 "2024-06-25T14:26:53Z")

</div>

Ha, that’s nice. I was struggling with the same problem. I want to charge the car during the 5 cheapest hours of the night. This can be done well with PBTH, but if you enter a fixed value for the period (10 hours, for example) and the price is lower during the day than at night, the 5 hours will continuously lie in the future. So, it is important to narrow the window so that it stops at the end time.

I managed to do it with a script that calculates the number of remaining hours until the deadline. Ideally, I would also like to shrink the 5 lowest, but for now, it meets my needs.

 ![afbeelding](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/8/2/82232defb5d1e3cb74149237338c94dd52e4f7f5.png)

The script:

```auto
// BepaalUren
const date = new Date();
var resterendeUren;
var doelUur = 8;
var huidigUur = date.getHours()+2; //schijnbaar werkt Homey met universele tijd
if (huidigUur > doelUur){ //het doeluur is morgen
resterendeUren = 24-huidigUur+doelUur;
} else {
resterendeUren = doelUur-huidigUur;
}
return resterendeUren;

```

---

<div class="post-metadata">

**Author:** ![evanhemmen](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/evanhemmen/32/79511_2.png) [@evanhemmen](https://community.homey.app/u/evanhemmen)\
**Post date:** [September 23, 2024, 8:12pm UTC](https://community.homey.app/t/calculate-number-of-hours-minutes-between-now-and-a-future-point-in-time/75617/18 "2024-09-23T20:12:06Z")

</div>

Thanks, this script works great! Exactly what I needed for practically the same usecase.

What do you mean with shrinking the 5 lowest?

---

<div class="post-metadata">

**Author:** ![PjotrAmslap](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/pjotramslap/32/79935_2.png) [@PjotrAmslap](https://community.homey.app/u/PjotrAmslap)\
**Post date:** [September 24, 2024, 10:37am UTC](https://community.homey.app/t/calculate-number-of-hours-minutes-between-now-and-a-future-point-in-time/75617/19 "2024-09-24T10:37:25Z")

</div>

At the beginning of the night, the car needs to charge for 5 hours. Now, this 5 hours remains a fixed value, but it should actually decrease throughout the night. So, if the car has already charged for an hour, it should ideally search for the 4 lowest-cost hours. Towards the end of the night, it only needs to charge for 1 or 2 more hours.

---

<div class="post-metadata">

**Author:** ![evanhemmen](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/evanhemmen/32/79511_2.png) [@evanhemmen](https://community.homey.app/u/evanhemmen)\
**Post date:** [September 24, 2024, 10:59am UTC](https://community.homey.app/t/calculate-number-of-hours-minutes-between-now-and-a-future-point-in-time/75617/20 "2024-09-24T10:59:09Z")

</div>

Ah clear.

In my situation 20% increase in battery level is around 1 hour of charging. Fortunately I’m able to import the current battery level of my car in Homey and with a very simple calculation I’m able to dynamically feed the remaining hours into the PBTH card.

So when the car is connected to the charger Homey calculates every hour:

(target % - actual %)/20

And feed that answer into the PBTH card.

Target % is a variable I choose at the beginning of a charging session with Dummy buttons.

---

<div class="post-metadata">

**Author:** ![Patrick\_Van\_Deursen](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/patrick_van_deursen/32/56539_2.png) [@Patrick\_Van\_Deursen](https://community.homey.app/u/Patrick_Van_Deursen)\
**Post date:** [September 24, 2024, 1:26pm UTC](https://community.homey.app/t/calculate-number-of-hours-minutes-between-now-and-a-future-point-in-time/75617/21 "2024-09-24T13:26:33Z")

</div>

add a timer that counts down when charging and stops when charging stops? The remaing timer you can read out and use I suppose
