# How to get the date for the last Monday from today?

**URL:** <https://discourse.nodered.org/t/how-to-get-the-date-for-the-last-monday-from-today/55857>\
**Category:** General\
**Created:** [29 December 2021 17:17 UTC](https://discourse.nodered.org/t/how-to-get-the-date-for-the-last-monday-from-today/55857 "2021-12-29T17:17:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Romprod](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@Romprod](https://discourse.nodered.org/u/Romprod)\
**Post date:** [29 December 2021 17:17 UTC](https://discourse.nodered.org/t/how-to-get-the-date-for-the-last-monday-from-today/55857/1 "2021-12-29T17:17:01Z")

</div>

Hey guys,

I'm looking for an automated way to get the date for the previous Monday from 'today'

So for example if I check now it would return 27-12-2021 and if I run it in 7 days time it would be 03-12-2021 etc etc

Is anyone able to suggest any nodes that may be able to do this that they're aware off or maybe a JS script way to do this?

---

<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:** [29 December 2021 17:29 UTC](https://discourse.nodered.org/t/how-to-get-the-date-for-the-last-monday-from-today/55857/2 "2021-12-29T17:29:40Z")

</div>

Using the change node and $moment() in a JSONata expression

```auto
[{"id":"21e5e450.ab0664","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment().isoWeekday(-6)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":440,"wires":[["95b09e83.cfbe38"]]},{"id":"4dd7f307.2c34ec","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":440,"wires":[["21e5e450.ab0664"]]},{"id":"95b09e83.cfbe38","type":"debug","z":"c791cbc0.84f648","name":"b","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":670,"y":340,"wires":[]}]

```

expression

```auto
$moment().isoWeekday(-6)

```

[edit] sorry that gets you previous to get the last of the month try

```auto
$moment().endOf('month').startOf('isoweek')

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [29 December 2021 17:35 UTC](https://discourse.nodered.org/t/how-to-get-the-date-for-the-last-monday-from-today/55857/3 "2021-12-29T17:35:11Z")

</div>

I'm sure there may be easier ways using libraries like `moment`, but in vanilla JavaScript, you can do:

```auto
let today = new Date();
let previousMonday = new Date(today.getTime() - (((today.getDay()+6)%7) * 1000*60*60*24))

```

Let's break that down.

1. First we get a `Date` object for today.
2. `today.getDay()` gives us the day of week, with 0=Sunday, 1=Monday etc
3. We can use that value to know how many days to go back to get to the previous Monday. Given its 0 indexed from Sunday, we need to subtract 1 from the value so that 0 is Monday... whilst ensuring that Sunday becomes a 6. That's what this bit of modulo maths does: `(today.getDay()+6)%7)`
4. Now we know how many days to go back, we get the current time in milliseconds (`today.getTime()`), then subtract the right number of days worth of milliseconds (`1000*60*60*24`)
5. Finally we create a new Date object from that time.

---

<div class="post-metadata">

**Author:** ![Romprod](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@Romprod](https://discourse.nodered.org/u/Romprod)\
**Post date:** [31 December 2021 15:35 UTC](https://discourse.nodered.org/t/how-to-get-the-date-for-the-last-monday-from-today/55857/4 "2021-12-31T15:35:58Z")

</div>

In the end I used this instead.

```auto
$moment().isoWeekday("Monday")

```

What that be an issue for any reason?

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [31 December 2021 17:28 UTC](https://discourse.nodered.org/t/how-to-get-the-date-for-the-last-monday-from-today/55857/5 "2021-12-31T17:28:15Z")

</div>

Use whichever option you wish.

It's sometimes easier to use a library such as moment, but be aware that 'moment' is being depreciated, and possibly succeeded by a different time/date library in the near future.

Also, you are missing out on a great JavaScript learning experience, which may help you in the future...

---

<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:** [1 March 2022 17:28 UTC](https://discourse.nodered.org/t/how-to-get-the-date-for-the-last-monday-from-today/55857/6 "2022-03-01T17:28:26Z")

</div>

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