FM
FlowMarket
General Automation Beginner ⚡ 2 n8n Nodes

Get local datetime into Function node using moment.js

!workflow-screenshot A quick example showing how to get the local date and time into a Function node using moment.js. This relies on the GENERICTIMEZONE envi...

🔌 Nodes & Integrations Used (1)

n8n Core Logic

📖 Implementation & Setup Guide

![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 `GENERIC_TIMEZONE` environment variable being correctly configured (see the docs [here](https://docs.n8n.io/reference/configuration.html#timezone))

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

```bash NODE_FUNCTION_ALLOW_EXTERNAL=moment ```

For convenience, the Function code is as follows:

```javascript 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 } } ]; ```

⚡ How to import this workflow into n8n:

  1. Click the Direct Download JSON button above to save Get local datetime into Function node using moment.js.json.
  2. Log in to your n8n Cloud or self-hosted n8n instance.
  3. In your workflow canvas, click the menu in top-right and select Import from File (or paste the JSON directly).
  4. Configure your credentials (e.g. API keys, database URLs) in the corresponding node settings.
  5. Click Execute Workflow to test and switch to Active!

Related General Automation Workflows

View All in General Automation →