# Help with Formatting and converting time string

**URL:** <https://discourse.nodered.org/t/help-with-formatting-and-converting-time-string/40471>\
**Category:** General\
**Created:** [6 February 2021 00:37 UTC](https://discourse.nodered.org/t/help-with-formatting-and-converting-time-string/40471 "2021-02-06T00:37:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [6 February 2021 00:37 UTC](https://discourse.nodered.org/t/help-with-formatting-and-converting-time-string/40471/1 "2021-02-06T00:37:57Z")

</div>

Hey Guys

I have a Time that is being posted by my pool system which i am dragging down from the internet

I end up with a string

2021-02-05T17:59:11.0000000Z from the HTTP request

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/6/46274d209d7fe8c287cbe2bc9caa322c120fe7ea.png)

I want to do some maths on this to see if it has been more than 4 hours since the last update

I have successfully fed this into the moment node and can convert it to local time, but i think i also want to get an output in ms since epoch so i can do the maths on it

Any pointers on setting up the moment node for this ??

Craig

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [6 February 2021 00:56 UTC](https://discourse.nodered.org/t/help-with-formatting-and-converting-time-string/40471/2 "2021-02-06T00:56:39Z")

</div>

What _message_ are you getting?

Is it a `timestamp`?

Because if it is, all you would need to do is look at the old value and the new value.  
If there is more than (4 hours = 240 minutes = 14400 seconds =) 14400000 milliseconds difference.

Just a thought.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [6 February 2021 06:36 UTC](https://discourse.nodered.org/t/help-with-formatting-and-converting-time-string/40471/3 "2021-02-06T06:36:26Z")

</div>

It is not a timestamp - it is exactly as i have put above - this is returned to me through an MQTT string from a process that does a Rest API query against a web based datasource.

I have no control of any of that unitl it is received by my MQTT broker.

As you can see in the shot above this is a string of exactly the format as listed.

So what i have done upto now (for display purposes) is to feed that through the moment node and place it into a text object on the dashboard.

What i am wanting to do now is to essentially reverse that and take that string, turn it back into a timestamp in ms and then perform maths on that to enable me to send an alert out.

So in essence the question is - how can i take that string and output a timestamp ?

Craig

---

<div class="post-metadata">

**Author:** ![Bobo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobo/32/8401_2.png) [@Bobo](https://discourse.nodered.org/u/Bobo)\
**Post date:** [6 February 2021 06:54 UTC](https://discourse.nodered.org/t/help-with-formatting-and-converting-time-string/40471/4 "2021-02-06T06:54:30Z")

</div>

> [@craigcurtin](#):
>
> So in essence the question is - how can i take that string and output a timestamp ?

In a function node (sorry not the Moment node), date.parse to convert from your string format to date format. Then you can use get.ms or whatever you want to do (eg [https://javascript.info/date](https://javascript.info/date))

---

<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:** [6 February 2021 07:25 UTC](https://discourse.nodered.org/t/help-with-formatting-and-converting-time-string/40471/5 "2021-02-06T07:25:45Z")

</div>

This should give you the timestamp in ms.

```auto
let ms = new Date(msg.payload).getTime()

```

---

<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 April 2021 07:26 UTC](https://discourse.nodered.org/t/help-with-formatting-and-converting-time-string/40471/6 "2021-04-07T07:26:27Z")

</div>

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