# Determine tomorrow's day of the month

**URL:** <https://discourse.nodered.org/t/determine-tomorrows-day-of-the-month/72718>\
**Category:** General\
**Tags:** function-node\
**Created:** [26 December 2022 10:50 UTC](https://discourse.nodered.org/t/determine-tomorrows-day-of-the-month/72718 "2022-12-26T10:50:35Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![scsirob](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@scsirob](https://discourse.nodered.org/u/scsirob)\
**Post date:** [26 December 2022 10:50 UTC](https://discourse.nodered.org/t/determine-tomorrows-day-of-the-month/72718/1 "2022-12-26T10:50:35Z")

</div>

I'm running Node Red v3.0.2 in a Linux VM

I'm trying to write some code to determine tomorrow's day of the month. One of my goals is to be as lean as possible, as this will run in a small system later. So to avoid adding large contributions, my approach is to get today's time, add 18400000 milliseconds to it (to get same time tomorrow), then get the date for that time. Here's my code:

```auto
var today = new Date().getTime();
var nDate = new Date().getDate();

var tomorrow = today + 18400000;
var tDate = new Date(tomorrow).getDate();

node.warn("Today = " + today + ", date = " + nDate);
node.warn("Tomorrow = " + tomorrow + ", date = " + tDate);

```

This worked a few times, but now I'm getting that today and tomorrow are the same:  
Today = 1672050837310, date = 26  
Tomorrow = 1672069237310, date = 26

The timestamp difference seems OK, but the date returned is not.  
Any hints on what I'm doing wrong?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 December 2022 11:21 UTC](https://discourse.nodered.org/t/determine-tomorrows-day-of-the-month/72718/3 "2022-12-26T11:21:48Z")

</div>

Welcome to the forum @scsirob  
The first one is 10:34 today and the second is 15:40 (both today GMT). You can check these at [https://www.epochconverter.com/](https://www.epochconverter.com/)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 December 2022 11:23 UTC](https://discourse.nodered.org/t/determine-tomorrows-day-of-the-month/72718/4 "2022-12-26T11:23:08Z")

</div>

> [@scsirob](#):
>
> to get today's time, add 18400000 milliseconds to it (to get same time tomorrow)

Recheck your maths there. Something is wrong with that figure.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [26 December 2022 11:55 UTC](https://discourse.nodered.org/t/determine-tomorrows-day-of-the-month/72718/5 "2022-12-26T11:55:57Z")

</div>

If you create a time object you can then minipulate as you need  
e.g.

```auto
msg.payload = {};
const time_obj = new Date(); //create time/date object
msg.payload.OG_time_object = RED.util.cloneMessage(time_obj);
msg.payload.time_milisecond_today = time_obj.getTime(); // us time object to get milliseconds
time_obj.setDate(time_obj.getDate() + 1) // add 1 day to time object
msg.payload.new_updated_time_obj = time_obj;
msg.payload.tomorrows_date = time_obj.getDate(); // get tomorrows date
return msg;

```

---

<div class="post-metadata">

**Author:** ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)\
**Post date:** [26 December 2022 12:04 UTC](https://discourse.nodered.org/t/determine-tomorrows-day-of-the-month/72718/6 "2022-12-26T12:04:34Z")

</div>

using moment

```auto
[{"id":"da90eebca55dbd3c","type":"moment","z":"1d77a0662bab21bb","name":"","topic":"","input":"","inputType":"date","inTz":"Europe/Berlin","adjAmount":"1","adjType":"days","adjDir":"add","format":"DD","locale":"DE-de","output":"","outputType":"msg","outTz":"Europe/Berlin","x":860,"y":480,"wires":[["05498e6eb3a39768"]]},{"id":"0c93d93f16d3c3db","type":"inject","z":"1d77a0662bab21bb","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":660,"y":480,"wires":[["da90eebca55dbd3c"]]},{"id":"05498e6eb3a39768","type":"debug","z":"1d77a0662bab21bb","name":"debug 42","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1060,"y":480,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [26 December 2022 12:09 UTC](https://discourse.nodered.org/t/determine-tomorrows-day-of-the-month/72718/7 "2022-12-26T12:09:41Z")

</div>

Using moment() without installing moment node, using a change node and a JSONata expression

```auto
$moment().add(1,"day").format("D")

```

example flow

```auto
[{"id":"0ccc963f1de2b66d","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":660,"wires":[["95f032d857efa5ed"]]},{"id":"95f032d857efa5ed","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment().add(1,\"day\").format(\"D\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":660,"wires":[["441b83f51eec6037"]]},{"id":"441b83f51eec6037","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 116","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":660,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 December 2022 12:14 UTC](https://discourse.nodered.org/t/determine-tomorrows-day-of-the-month/72718/8 "2022-12-26T12:14:21Z")

</div>

I think the guy just needs to the number of msec in a day correct.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [26 December 2022 12:15 UTC](https://discourse.nodered.org/t/determine-tomorrows-day-of-the-month/72718/9 "2022-12-26T12:15:44Z")

</div>

> [@Colin](#):
>
> I think the guy just needs to the number of msec in a day correct.

> [@scsirob](#):
>
> I'm trying to write some code to determine tomorrow's day of the month.

Not according to his first sentence in second paragraph. He also needs to understand you create a time object, then you can manipulate it as long as you do not overwrite or convert its type. For that he does not need any milliseconds.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 December 2022 12:17 UTC](https://discourse.nodered.org/t/determine-tomorrows-day-of-the-month/72718/10 "2022-12-26T12:17:23Z")

</div>

Do you mean his code will not work as written (with the right msec offset). Why not?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [26 December 2022 12:19 UTC](https://discourse.nodered.org/t/determine-tomorrows-day-of-the-month/72718/11 "2022-12-26T12:19:17Z")

</div>

No i mean he does not require milliseconds to get tomorrows date from a time object. But if milliseconds were correct it would work also, obviously.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 December 2022 12:34 UTC](https://discourse.nodered.org/t/determine-tomorrows-day-of-the-month/72718/12 "2022-12-26T12:34:13Z")

</div>

Agreed, though obviously there is a small possibility of error with the msec approach over the DST boundary (when there are 23 or 25 hours in the day).

---

<div class="post-metadata">

**Author:** ![scsirob](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@scsirob](https://discourse.nodered.org/u/scsirob)\
**Post date:** [26 December 2022 13:14 UTC](https://discourse.nodered.org/t/determine-tomorrows-day-of-the-month/72718/13 "2022-12-26T13:14:07Z")

</div>

Thanks all for the comments.  
Duh.... Wrong milliseconds. Need to use 86400000 instead.  
The accuracy doesn't need to be in milliseconds. The resulting date is used to iterate through an array that was retrieved from a web API call, with date, time and information entries for each hour. The array is multiple days ahead, with this function I filter just tomorrow's data.

Setting the time object to one day further as @E1cid described also works and is less code, so I will use that. I'm used to writing in C, learning a new language is a bit of a hill for me.

This now works, thanks!

---

<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:** [9 January 2023 13:14 UTC](https://discourse.nodered.org/t/determine-tomorrows-day-of-the-month/72718/14 "2023-01-09T13:14:26Z")

</div>

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