# Getting Today's Date

**URL:** <https://discourse.nodered.org/t/getting-todays-date/54533>\
**Category:** General\
**Created:** [30 November 2021 20:54 UTC](https://discourse.nodered.org/t/getting-todays-date/54533 "2021-11-30T20:54:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rlongfield](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rlongfield/32/14264_2.png) [@rlongfield](https://discourse.nodered.org/u/rlongfield)\
**Post date:** [30 November 2021 20:54 UTC](https://discourse.nodered.org/t/getting-todays-date/54533/1 "2021-11-30T20:54:28Z")

</div>

I am trying to get the days date whenever a flow is run and store that date in slots.date.

I tried this, the code was modified from a get time node.

```auto
var dateString = new Date().toLocaleTimeString([],
{
    day: "2-digit", 
    month: "2-digit"
})

{
msg.slots.date = dateString;
}
return msg;

```

I get the current date but it also includes the time, which I do not want. I would like to change the format of the date from the 11/30 to Tues Nov 30.  
I looked at some JS docs but everything I found was using Date() to set the date not retrieve it.

---

<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:** [30 November 2021 21:15 UTC](https://discourse.nodered.org/t/getting-todays-date/54533/2 "2021-11-30T21:15:00Z")

</div>

> [@rlongfield](#):
>
> I looked at some JS docs but everything I found was using Date() to set the date not retrieve it.

They can't have been comprehensive docs then. Look for the `get...` functions here [Date - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)

---

<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:** [30 November 2021 21:20 UTC](https://discourse.nodered.org/t/getting-todays-date/54533/3 "2021-11-30T21:20:28Z")

</div>

> [@rlongfield](#):
>
> I would like to change the format of the date from the 11/30 to Tues Nov 30.

An alternative using a Change node with a Jsonata [$moment()](https://momentjscom.readthedocs.io/en/latest/moment/04-displaying/01-format/) expression

```auto
[{"id":"57ce8155a9b3ed3a","type":"change","z":"54efb553244c241f","name":"","rules":[{"t":"set","p":"slots.date","pt":"msg","to":"$moment().format('ddd MMM DD')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":900,"wires":[["e952d58798dbf0d3"]]},{"id":"7efd9ae22d4311e5","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":350,"y":900,"wires":[["57ce8155a9b3ed3a"]]},{"id":"e952d58798dbf0d3","type":"debug","z":"54efb553244c241f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":740,"y":900,"wires":[]}]

```

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/9/59544f13d942bd5e555a81718204c3ab5f23b39d.png)

---

<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:** [30 November 2021 21:25 UTC](https://discourse.nodered.org/t/getting-todays-date/54533/4 "2021-11-30T21:25:01Z")

</div>

Or return the Date() as a string and split it to return the first 3 elements, then rejoin them. Or as Unborn says use JSONata and moments

```auto
[{"id":"f6a7dd1b.111578","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"accX\":-0.123456,\"accY\":1.123456,\"accZ\":4.123456}","payloadType":"json","x":90,"y":500,"wires":[["9526971f.125058","1ba7310.f637acf"]]},{"id":"9526971f.125058","type":"function","z":"c791cbc0.84f648","name":"","func":"var dateString = new Date().toString().split(\" \", 3).join(\" \");\nmsg.slots= {date: dateString}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":420,"wires":[["f3f50673.aac85"]]},{"id":"1ba7310.f637acf","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"slots.date","pt":"msg","to":"$moment().tz(\"euroupe/London\").format(\"ddd MMM Do\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":540,"wires":[["f3f50673.aac85"]]},{"id":"f3f50673.aac85","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"slots.date","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":420,"wires":[]}]

```

[edit] I added how to set localtime and the st, nd and th to the day of the month.  
[https://momentjs.com/docs/#/displaying/format/](https://momentjs.com/docs/#/displaying/format/)

> **[List of tz database time zones](https://en.m.wikipedia.org/wiki/List_of_tz_database_time_zones)**
>
> This is a list of time zones from release 2021e of the tz database.
> UTC offsets (columns 6 and 7) are positive east of UTC and negative west of UTC. The UTC DST offset is different from the UTC offset for zones where daylight saving time is observed (see individual time zone pages for details). The UTC offsets are for the current or upcoming rules, and may have been different in the past.
> The "Type" field means:

---

<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:** [29 January 2022 21:25 UTC](https://discourse.nodered.org/t/getting-todays-date/54533/5 "2022-01-29T21:25:10Z")

</div>

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