Get local datetime into Function node using moment.js
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:
NODE_FUNCTION_ALLOW_EXTERNAL=moment
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 = 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 } } ]; `
Related Templates
Extract Title tag and Meta description from url for SEO analysis with Airtable
Extract Title tag and meta description from url for SEO analysis. How it works The workflows takes records from Airtabl...
Restore your workflows from GitHub
This workflow restores all n8n instance workflows from GitHub backups using the n8n API node. It complements the Backup ...
Extract Named Entities from Web Pages with Google Natural Language API
Who is this for? Content strategists analyzing web page semantic content SEO professionals conducting entity-based anal...
🔒 Please log in to import templates to n8n and favorite templates
Workflow Visualization
Loading...
Preparing workflow renderer
Comments (0)
Login to post comments