Back to Workflows

Get local datetime into Function node using moment.js

Apps in this workflow


Building Blocks



Created 2 years ago by trey
eye 52 views

A quick example showing how to get the local date and time into a Function node using moment.js.

This relies on the GENERIC_TIMEZONE environment variable being correctly configured (see the docs here)

NOTE: In order for this to work, you must whitelist the moment library for use by setting the following environment variable:


For convenience, the Function code is as follows:

const moment = require('moment');

let date = moment().tz($env['GENERIC_TIMEZONE']);

let year = date.year();
let month = date.month(); // zero-indexed!
let day =;
let hour = date.hours();
let minute = date.minutes();
let second = date.seconds();
let millisecond = date.millisecond();
let formatted = date.format('YYYY-MM-DD HH:mm:ss.SSS Z');

return [
    json: {
      utc: date,
      year: year,
      month: month, // zero-indexed!
      day: day,
      hour: hour,
      minute: minute,
      second: second,
      millisecond: millisecond,
      formatted: formatted

Implement complex processes faster with n8n

red icon yellow icon red icon yellow icon