Get local datetime into Function node using moment.js
Needs changesNeeds 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.0 | Replace with |
|---|---|
| Function (legacy) Get Local Datetime | Code node, mode 'Run Once for All Items' |
Nodes used (2)
- Manual Trigger
n8n-nodes-base.manualTrigger - Function
n8n-nodes-base.function
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 } } ;