# Calculate No days Remaining to pay the dues?

**URL:** <https://discourse.nodered.org/t/calculate-no-days-remaining-to-pay-the-dues/33208>\
**Category:** General\
**Created:** [23 September 2020 03:53 UTC](https://discourse.nodered.org/t/calculate-no-days-remaining-to-pay-the-dues/33208 "2020-09-23T03:53:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ceaswaran](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ceaswaran/32/28975_2.png) [@ceaswaran](https://discourse.nodered.org/u/ceaswaran)\
**Post date:** [23 September 2020 03:53 UTC](https://discourse.nodered.org/t/calculate-no-days-remaining-to-pay-the-dues/33208/1 "2020-09-23T03:53:39Z")

</div>

I have a sensor that pools the payment due date in the following format `12/10/20` (DD/MM/YY- 12th October 2020) from my government's Electricity board server.

I want to create an output in Node-red which will find/calculate the Number days Remaining to pay the dues. (i.e 12th October 2020 - today's date/23 Sep 2020)

How do I achieve it in Node-red?

Please help

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [23 September 2020 06:11 UTC](https://discourse.nodered.org/t/calculate-no-days-remaining-to-pay-the-dues/33208/2 "2020-09-23T06:11:18Z")

</div>

Hello,  
If you are comfortable with some basic javascript you could use the build in date parsing function of javascript in a function node:

> **[Date.parse()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse)**
>
> The Date.parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31).

  
This gives you the date input parsed to standard milliseconds since midnight of the 1st of January 1970.  
If you do this for both dates and save the results in two variables you can than by simple subtraction calculate the difference in milliseconds and divide the result by the number of milliseconds in a day ( 86400000) to get the number of days between the two days.

Johannes

---

<div class="post-metadata">

**Author:** ![ceaswaran](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ceaswaran/32/28975_2.png) [@ceaswaran](https://discourse.nodered.org/u/ceaswaran)\
**Post date:** [23 September 2020 08:32 UTC](https://discourse.nodered.org/t/calculate-no-days-remaining-to-pay-the-dues/33208/3 "2020-09-23T08:32:08Z")

</div>

> [@JGKK](#):
>
> 86400000

Thanks, I am not comfortable in java but I think I can manage COPY PASTE code & understand its flow

As per my understanding the `Date.parse()` needs date in YY/MMDD but my Sensor give the date in DD/MM/YY format hence am not able to prase the date or i get a wrong No of days.  
Is there a trick to fix it?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 September 2020 09:30 UTC](https://discourse.nodered.org/t/calculate-no-days-remaining-to-pay-the-dues/33208/4 "2020-09-23T09:30:05Z")

</div>

> [@ceaswaran](#):
>
> I am not comfortable in java

Hi @ceaswaran this is JavaScript NOT java. There is a small difference in name but a huge difference in the acutal languages (i.e. searching for a java solution will almost always NOT work in node-red that uses JavaScript.

Use a function to convert the string to date...

```auto
var dateString = msg.payload; // << adjust to where ever your date string is in the msg
var dateParts = dateString.split("/"); // split on /

// month is 0-based, that's why we need dataParts[1] - 1
var d = new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0]); 

var now = new Date(); //get now date
var diff = now - d; //difference in ms
var days = diff / 86400000;

msg.payload = days; //store the days value in the msg, ready to send to next node

return msg; // send the msg to the next node

```

---

<div class="post-metadata">

**Author:** ![ceaswaran](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ceaswaran/32/28975_2.png) [@ceaswaran](https://discourse.nodered.org/u/ceaswaran)\
**Post date:** [23 September 2020 10:05 UTC](https://discourse.nodered.org/t/calculate-no-days-remaining-to-pay-the-dues/33208/5 "2020-09-23T10:05:37Z")

</div>

Thanks , it works  
The only thing is I need to add 2000 to year so it takes year as 2020 not 1920

---

<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:** [7 October 2020 10:05 UTC](https://discourse.nodered.org/t/calculate-no-days-remaining-to-pay-the-dues/33208/6 "2020-10-07T10:05:39Z")

</div>

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