# Function for calculating value that changes with date

**URL:** <https://discourse.nodered.org/t/function-for-calculating-value-that-changes-with-date/39214>\
**Category:** General\
**Created:** [16 January 2021 02:38 UTC](https://discourse.nodered.org/t/function-for-calculating-value-that-changes-with-date/39214 "2021-01-16T02:38:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkenobi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bkenobi/32/59825_2.png) [@bkenobi](https://discourse.nodered.org/u/bkenobi)\
**Post date:** [16 January 2021 02:38 UTC](https://discourse.nodered.org/t/function-for-calculating-value-that-changes-with-date/39214/1 "2021-01-16T02:38:13Z")

</div>

My goal is to have NR update the cost per kWh based on the schedule that has been published by my utility provider. Currently they have published a pdf that shows the $/kWh for time periods through 2025. I would like to check the date (probably date.now) and determine what block it fits into and apply the appropriate rate. I'm not a JS programmer so this is not in my wheel house. I've found some code snippets but it appears I may be over complicating things.

```auto
var from = new Date("02/08/2017").getTime();
var to = new Date("05/08/2017").getTime();
var withinRange = today >= from && today <= to;

```

And here's the rate table:

```auto
effective date $/kWh
1 Oct 2017 0.10414
1 Apr 2021 0.10118
1 Apr 2022 0.09822
1 Apr 2023 0.09527
1 Apr 2024 0.09231
1 Apr 2025 0.08935

```

I would typically use an if-elseif structure, but I'm not sure if there's a better way in JS.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [16 January 2021 02:53 UTC](https://discourse.nodered.org/t/function-for-calculating-value-that-changes-with-date/39214/2 "2021-01-16T02:53:54Z")

</div>

So are the rate structures more complicated than what you have listed or is the list above the real list ?

(Wish i lived where you were and got those rates !!)

If the list above is it - then i do not know that i would overly complicate it - they are only going to change once per year on April 1st.

What are you wanting to achieve once you have this ?

Craig

---

<div class="post-metadata">

**Author:** ![bkenobi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bkenobi/32/59825_2.png) [@bkenobi](https://discourse.nodered.org/u/bkenobi)\
**Post date:** [16 January 2021 05:13 UTC](https://discourse.nodered.org/t/function-for-calculating-value-that-changes-with-date/39214/3 "2021-01-16T05:13:23Z")

</div>

Yeah, this is the actual rates. I just want to set the rate and then multiply by the kWh that I feed to it to determine the accumulated cost. I tried a basic approach but my JavaScript isn't that strong.

```auto
const now = Date.now();
var rate = -1;

if(now <= Date("1/4/2021")) {
   rate = 0.10414;
}
else if(now <= Date("1/4/2022")) {
   rate = 0.10118;
}
else if(now <= Date("1/4/2023")) {
   rate = 0.09822;
}
else if(now <= Date("1/4/2024")) {
   rate = 0.09527;
}
else if(now <= Date("1/4/2025")) {
   rate = 0.08935;
}

//msg.payload = msg.payload * rate;
msg.payload = rate;
return msg;

```

The function always returns -1 so I'm clearly not using either the date functions wrong.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [16 January 2021 06:37 UTC](https://discourse.nodered.org/t/function-for-calculating-value-that-changes-with-date/39214/4 "2021-01-16T06:37:34Z")

</div>

Hi .. your research to use getTime() when comparing Dates is correct. you have to apply it to your code.  
Also based on the [MDN article on creating dates](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#several_ways_to_create_a_date_object) from strings, im not sure the slash (/) and the order of you string is valid.

```auto
const now = new Date().getTime();
node.warn(now)
var rate = -1;

if(now <= new Date("2021-4-1").getTime()) {
  rate = 0.10414;
}
else if(now <= new Date("2022-4-1").getTime()) {
  rate = 0.10118;
}
else if(now <= new Date("2023-4-1").getTime()) {
  rate = 0.09822;
}
else if(now <= new Date("2024-4-1").getTime()) {
  rate = 0.09527;
}
else if(now <= new Date("2025-4-1").getTime()) {
  rate = 0.08935;
}

//msg.payload = msg.payload * rate;
msg.payload = rate;
return msg;

```

---

<div class="post-metadata">

**Author:** ![bkenobi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bkenobi/32/59825_2.png) [@bkenobi](https://discourse.nodered.org/u/bkenobi)\
**Post date:** [16 January 2021 17:16 UTC](https://discourse.nodered.org/t/function-for-calculating-value-that-changes-with-date/39214/5 "2021-01-16T17:16:29Z")

</div>

🤦‍♂️  
I didn't check the acceptable date format. Thanks for the updated code, it seems to work great!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [17 March 2021 17:16 UTC](https://discourse.nodered.org/t/function-for-calculating-value-that-changes-with-date/39214/6 "2021-03-17T17:16:40Z")

</div>

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