Templates / Get local datetime into Function node using moment.js

Get local datetime into Function node using moment.js

Needs changes

by Trey ✓ · 1,986 views · created 2020-10-01 · Engineering

Needs changes. This template uses 1 thing(s) that n8n 3.0 removes: Function (legacy). 1 breaking item(s) found. Static check, last run 2026-10-19.

What to replace before upgrading

Removed in 3.0Replace with
Function (legacy)
Get Local Datetime
Code node, mode 'Run Once for All Items'

Nodes used (2)

What it does

!workflow-screenshot(fileId:260) A quick example showing how to get the local date and time into a Function node using moment.js. This relies on the GENERICTIMEZONE 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: bash NODEFUNCTIONALLOWEXTERNAL=moment For convenience, the Function code is as follows: javascript const moment = require('moment'); let date = moment().tz($env'GENERICTIMEZONE'); let year = date.year(); let month = date.month(); // zero-indexed! let day = date.date(); 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 } } ;

View the original on n8n.io → Check your own copy

Get the weekly “what broke” email

New templates, newly broken ones, and n8n release changes. One short email a week.

Form not loading? Subscribe here.