# Date not working if flow context not set

**URL:** <https://discourse.nodered.org/t/date-not-working-if-flow-context-not-set/48762>\
**Category:** General\
**Created:** [22 July 2021 09:24 UTC](https://discourse.nodered.org/t/date-not-working-if-flow-context-not-set/48762 "2021-07-22T09:24:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![maraguma](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maraguma/32/42793_2.png) [@maraguma](https://discourse.nodered.org/u/maraguma)\
**Post date:** [22 July 2021 09:24 UTC](https://discourse.nodered.org/t/date-not-working-if-flow-context-not-set/48762/1 "2021-07-22T09:24:22Z")

</div>

Hi, I have this bit of code in a function node. In line 1, I want to set the `lastTimestamp` to now if the flow context store is not set, but it doesn't work.

If I do the same (line 2) without getting it from the flow context, it works. What am I doing wrong?

```auto
var lastTimestamp = new Date(flow.get("lastTimestamp")) || Date.now();
var now = Date.now();

msg.payload = lastTimestamp;
msg.now = now;

return msg;

```

![Bildschirmfoto 2021-07-22 um 11.14.59](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/6/666bf09e81480eb779bf56999188cdbdb236a800.png)

---

<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:** [22 July 2021 09:30 UTC](https://discourse.nodered.org/t/date-not-working-if-flow-context-not-set/48762/2 "2021-07-22T09:30:23Z")

</div>

If `flow.get("lastTimestamp")` returns undefined, then you are doing:

```auto
new Date(undefined)

```

Which results in an invalid Date object, but its not a 'false-like' value. That means the `||` operator doesn't do anything.

I think the following does what you want:

```auto
var lastTimestamp = new Date(flow.get("lastTimestamp") || Date.now() );

```

---

<div class="post-metadata">

**Author:** ![maraguma](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maraguma/32/42793_2.png) [@maraguma](https://discourse.nodered.org/u/maraguma)\
**Post date:** [22 July 2021 09:36 UTC](https://discourse.nodered.org/t/date-not-working-if-flow-context-not-set/48762/3 "2021-07-22T09:36:58Z")

</div>

Thanks, that did the trick.

---

<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:** [5 August 2021 09:37 UTC](https://discourse.nodered.org/t/date-not-working-if-flow-context-not-set/48762/4 "2021-08-05T09:37:39Z")

</div>

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