I’ve had a lot of “fun” with DateTime, formatting dates and time, timestamps recently and this article is the best resource I found:
Here is the snippet from the article which I use in a function node to format date time however I or a third party API needs it:
Note the pad function which adds a 0 to single digit month or dates (2021-5-9 becomes 2021-05-09). I name a lot of files with this format and was struggling before how to get it to work in JavaScript. The months start at 0
const now = new Date();
function pad(n) {
return n<10 ? '0'+n : n
}
const localDateTime = now.getFullYear() +
"-" +
pad(now.getMonth()+1) +
"-" +
pad(now.getDate()) +
" " +
pad(now.getHours()) +
":" +
pad(now.getMinutes()) +
":" +
pad(now.getSeconds());
items[0].json.time = localDateTime
return items
I still have to understand how to master timezones but will get there eventually.
Best,
Chris