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

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

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;

toLocaleTimeString does exactly that makes a time string.

Use Date.prototype.toLocaleString() - JavaScript | MDN

or Intl.DateTimeFormat - JavaScript | MDN

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.

1 Like

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