# Corrent syntax to pull date, time, from timestamp and format to .csv

**URL:** <https://discourse.nodered.org/t/corrent-syntax-to-pull-date-time-from-timestamp-and-format-to-csv/56088>\
**Category:** General\
**Tags:** database\
**Created:** [4 January 2022 14:48 UTC](https://discourse.nodered.org/t/corrent-syntax-to-pull-date-time-from-timestamp-and-format-to-csv/56088 "2022-01-04T14:48:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Loonhaunt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/loonhaunt/32/34517_2.png) [@Loonhaunt](https://discourse.nodered.org/u/Loonhaunt)\
**Post date:** [4 January 2022 14:48 UTC](https://discourse.nodered.org/t/corrent-syntax-to-pull-date-time-from-timestamp-and-format-to-csv/56088/1 "2022-01-04T14:48:02Z")

</div>

I am using the code below in a format node to pull data from openweathermap to create a small .csv data base. I am having a hard time getting and formatting date time info into date,time or datetime, I am only able to extract time,? Is there a resource for syntax for these simple extract and string functions other than StackOverFlow?

Thanks,  
Loon

```auto
tvar first=context.get('first') || 0;
if(first == "0")
{
    context.set('first',1);
    All = " Time Temperature Humidity Pressure";
}

var LocalTime = new Date();
var Tim = LocalTime.toLocaleTimeString();
Temp = msg.payload.tempc;
Hum = msg.payload.humidity;
Pres = msg.payload.pressure;
if(first != "0")
   All = Tim + "," + parseFloat((Temp*1.8)+32).toFixed(2) + "," + Hum + ","+parseFloat((Pres)/33.86).toFixed(2);
msg.payload = All
return msg;

```

---

<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:** [4 January 2022 14:55 UTC](https://discourse.nodered.org/t/corrent-syntax-to-pull-date-time-from-timestamp-and-format-to-csv/56088/2 "2022-01-04T14:55:46Z")

</div>

`toLocaleTimeString` does exactly that makes a time string.

Use [Date.prototype.toLocaleString() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString)

or [Intl.DateTimeFormat - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat)

---

<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:** [4 January 2022 15:22 UTC](https://discourse.nodered.org/t/corrent-syntax-to-pull-date-time-from-timestamp-and-format-to-csv/56088/3 "2022-01-04T15:22:58Z")

</div>

Generally it is best not to store the date in local time, but in UTC, otherwise, for example, you will get repeated timestamps at the DST changeover. Put it in the file just by writing `new Date()` to the file and it should automatically put it in ISO format. Then conbert it to local time any time you need to display it.

---

<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 March 2022 15:23 UTC](https://discourse.nodered.org/t/corrent-syntax-to-pull-date-time-from-timestamp-and-format-to-csv/56088/4 "2022-03-05T15:23:20Z")

</div>

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