# Timestamp in function problems

**URL:** <https://discourse.nodered.org/t/timestamp-in-function-problems/34638>\
**Category:** General\
**Created:** [21 October 2020 14:19 UTC](https://discourse.nodered.org/t/timestamp-in-function-problems/34638 "2020-10-21T14:19:08Z")\
**Posts on this page:** 1\
**Showing post:** 9

<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:** [21 October 2020 17:47 UTC](https://discourse.nodered.org/t/timestamp-in-function-problems/34638/9 "2020-10-21T17:47:33Z")

</div>

Hi Orio,

I didnt study your javascript in depth but if it was me i would go to the original idea of using the **Moment js** library that makes datetime calculations easier with its extended library.

In order for the module library to be accesible is your function nodes you edit the **functionGlobalContext** section of the Node-red **settings.js** file :

```auto
 functionGlobalContext: {
           moment:require('moment')
        // os:require('os'),
        // jfive:require("johnny-five"),
        // j5board:require("johnny-five").Board({repl:false})
    },

```

**and in the Function node :**

```javascript
let moment = global.get('moment')

let currentTime = moment();
let sqlTime = moment('20201021075538','YYYYMMDDHHmmss'); // convert to moment
let duration = sqlTime.from(currentTime)

node.warn(currentTime)
node.warn(sqlTime)
node.warn(`Duration : ${duration}`)

//node.send({'payload': `This is the year: ${moment().format('YYYY')} as a string`})

```

**Example flow :**

```auto
[{"id":"bb4051ca.65a4e8","type":"inject","z":"54aa25ce.417e1c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":300,"y":380,"wires":[["345d113b.aaa426"]]},{"id":"345d113b.aaa426","type":"function","z":"54aa25ce.417e1c","name":"","func":"let moment = global.get('moment')\n\nlet currentTime = moment();\nlet sqlTime = moment('20101021075538','YYYYMMDDHHmmss');\nlet duration = sqlTime.from(currentTime)\n\nnode.warn(currentTime)\nnode.warn(sqlTime)\nnode.warn(`Duration : ${duration}`)\n\n//node.send({'payload': `This is the year: ${moment().format('YYYY')} as a string`})","outputs":1,"noerr":0,"initialize":"","finalize":"","x":490,"y":380,"wires":[["9cba50f8.4d01d8"]]},{"id":"9cba50f8.4d01d8","type":"debug","z":"54aa25ce.417e1c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":680,"y":380,"wires":[]}]

```

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

---

_[View the full topic](https://discourse.nodered.org/t/timestamp-in-function-problems/34638)._
