# Time stamp / get Time

**URL:** https://discourse.nodered.org/t/time-stamp-get-time/20504
**Category:** General
**Created:** [17 January 2020 15:38 UTC](https://discourse.nodered.org/t/time-stamp-get-time/20504 "2020-01-17T15:38:00Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Chris\_Wie](https://avatars.discourse-cdn.com/v4/letter/c/8dc957/32.png) [@Chris\_Wie](https://discourse.nodered.org/u/Chris_Wie)
#### Post date: [17 January 2020 15:38 UTC](https://discourse.nodered.org/t/time-stamp-get-time/20504/1 "2020-01-17T15:38:00Z")

</div>

Dear all,

I am trying to get some information from my old drilling machine.  
With the help of some ultrasonic sensors I would like to measure the distance of a part and also generate a timestamp so I know how long the part was resting under the drill head.

Therefore I created a code which is unfortunately not working.  
Task:  
The distance is uploaded every second, so far so good.  
If the distance is below 10 cm the part is set to 1 and as long as it is 1 time should be added.

My idea was to set a timestamp when it becomes 1 and another one when it becomes 0 and in the end I will subtract the 1 timestamp from the 0 timestamp.

Attached you can find my code

let part;  
let end;  
let start;  
let processingTime;

if (msg.payload.distancecm \< 10) {  
part = 1;  
} else {  
part = 0;  
}

if (part === 1) {  
start = new Date();  
} else {  
(part === 0){  
end = new Date();  
processingTime = end.getTime() - start.getTime();  
}  
}

msg.payload="[{"Timestamp" :"" + Date() + "","distance" :"" + msg.payload.distancecm + "","part" :"" + part + "","end" :"" + end + "","start" :"" + start + "","processingTime" :"" + processingTime + ""}]"  
return msg;

Thank you very much in advance for your help.

Greetings

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [17 January 2020 15:46 UTC](https://discourse.nodered.org/t/time-stamp-get-time/20504/2 "2020-01-17T15:46:04Z")

</div>

One thing, you have an if...else... but you have a conditional after the else so I think you want this

```auto
if (part === 1) {
start = new Date();
} else {
(part === 0){
end = new Date();
processingTime = end.getTime() - start.getTime();
}

```

to be

```auto
if (part === 1) {
start = new Date();
} else if (part === 0){
end = new Date();
processingTime = end.getTime() - start.getTime();
}

```

---

<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: [17 January 2020 16:10 UTC](https://discourse.nodered.org/t/time-stamp-get-time/20504/3 "2020-01-17T16:10:31Z")

</div>

Also you will need to save the start time in the node context and pick it up again when you determine the end time. Variables set in a function node are lost at the end of the function unless you save them in the context. Look in the node-red docs on writing functions to find details of how to do that.

---

<div class="post-metadata">

### Author: ![ChrisF](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@ChrisF](https://discourse.nodered.org/u/ChrisF)
#### Post date: [25 January 2023 21:10 UTC](https://discourse.nodered.org/t/time-stamp-get-time/20504/4 "2023-01-25T21:10:16Z")

</div>

Hi all, I have a window contact and I want when the window is closed, your timestamp is set.  
But the function only works if the function says "opened at" and then "closed at". Swap is it (as I need it) arises an error ☹

var date=Date.now();

var seconds=(((date % 3600000) % 60000) / 1000).toFixed(0);  
var minutes=((date % 3600000) / 60000).toFixed(0);  
var hours=date / 3600000;  
var ms=((date % 3600000) % 60000) % 1000;  
var days=hours / 24;  
var weeks=(days / 7).toFixed(0);  
days=(days % 7).toFixed(0);  
hours=(hours % 24).toFixed(0);

if (hours\<10)  
{  
hours= "0"+String(hours);  
}

if (minutes\<10)  
{  
minutes= "0"+String(minutes);  
}

if (seconds\<10)  
{  
seconds= "0"+String(seconds);  
}

//convert time:  
var time=String(hours) + ":" + String(minutes) + ":" + String(seconds);

var output= flow.get("doorContact1-State")? "opened at ("+time+")": "closed at ("+time+")";  
var msgOut= {payload: output};  
return msgOut;

I think I would just have to swap true and false

---

<div class="post-metadata">

### Author: ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)
#### Post date: [25 January 2023 21:58 UTC](https://discourse.nodered.org/t/time-stamp-get-time/20504/5 "2023-01-25T21:58:51Z")

</div>

Try

```auto
[{"id":"45661fb827a72921","type":"function","z":"f859c7434a67ede7","name":"function 3","func":"const date = new Date()\nconst doorContact1_State = msg.payload\n\n//convert time:\nconst time = date.toLocaleString()\n\nconst output= (doorContact1_State)? \"opened at (\"+time+\")\": \"closed at (\"+time+\")\"\n\nmsg.payload = output\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":740,"y":1060,"wires":[["b7bcecf8b14cacf8"]]},{"id":"058abd63956e9ab7","type":"inject","z":"f859c7434a67ede7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":490,"y":1060,"wires":[["45661fb827a72921"]]},{"id":"b7bcecf8b14cacf8","type":"debug","z":"f859c7434a67ede7","name":"debug 23","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":980,"y":1060,"wires":[]}]

```

the whole function can be reduced to (`msg.payload` is taking the place of your `flow context`)

```auto
const date = new Date()
const doorContact1_State = msg.payload

//convert time:
const time = date.toLocaleString()

const output= (doorContact1_State) ? "opened at (" + time + ")": "closed at (" + time + ")"

msg.payload = output

return msg

```

One thing to note, Javascript does not like `'-'`, use `'_'` instead, also you are mixing numbers and strings which will not work (`.toFixed(0)` gives a string, NOT a number)

---

<div class="post-metadata">

### Author: ![ChrisF](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@ChrisF](https://discourse.nodered.org/u/ChrisF)
#### Post date: [26 January 2023 21:51 UTC](https://discourse.nodered.org/t/time-stamp-get-time/20504/6 "2023-01-26T21:51:26Z")

</div>

Thank you that worked.  
Now I just have to configure the date. I do not like it yet

I would like it like this: 26.01.2023 / 22:53

---

<div class="post-metadata">

### Author: ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)
#### Post date: [27 January 2023 00:10 UTC](https://discourse.nodered.org/t/time-stamp-get-time/20504/7 "2023-01-27T00:10:23Z")

</div>

> [@ChrisF](#):
>
> 26.01.2023 / 22:53

What country format is this?

```auto
time = `${date.getDate()}.${date.getMonth() + 1}.${date.getFullYear()} / ${date.getHours()}:${date.getMinutes()}`

```

gives `27.1.2023 / 0:4`

or

```auto
const options = {
	day: '2-digit', 
  	month: '2-digit',
  	year: 'numeric', 
  	hour: '2-digit', 
  	minute: '2-digit', 
  	hour12: false

}

time = date.toLocaleString('de-DE', options).replace(', ', ' / ')

```

gives `27.01.2023 / 00:04`

---

<div class="post-metadata">

### Author: ![ChrisF](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@ChrisF](https://discourse.nodered.org/u/ChrisF)
#### Post date: [27 January 2023 06:02 UTC](https://discourse.nodered.org/t/time-stamp-get-time/20504/8 "2023-01-27T06:02:27Z")

</div>

> [@Buckskin](#):
>
> What country format is this?

Germany 🙂

Can I just paste that into the function you gave me above?

---

<div class="post-metadata">

### Author: ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)
#### Post date: [27 January 2023 15:42 UTC](https://discourse.nodered.org/t/time-stamp-get-time/20504/9 "2023-01-27T15:42:25Z")

</div>

Yes, like so;

```auto
const date = new Date()
const doorContact1_State = msg.payload

const options = {
    day: '2-digit',
    month: '2-digit',
    year: 'numeric',
    hour: '2-digit',
    minute: '2-digit',
    hour12: false

}

//convert time:
const time = date.toLocaleString('de-DE', options).replace(', ', ' / ')

const output = (doorContact1_State) ? "opened at (" + time + ")" : "closed at (" + time + ")"

msg.payload = output

return msg

```

The standard date/time format for the '`de-DE`' locale is `27.01.2023, 15:39` so if you want to stick to that just use `const time = date.toLocaleString('de-DE', options)`
