Who is this workflow for? This guide demonstrates how to retrieve the local date and time within an n8n Function node using the Moment.js library. By configuring the environment correctly, you can seamlessly integrate local time data into your automated workflows..

What does this workflow do?

  • Configure Environment Variables:
  • Set the GENERIC_TIMEZONE environment variable to your desired timezone.
  • To allow the use of the Moment.js library, set NODE_FUNCTION_ALLOW_EXTERNAL=moment.
  • Function Node Setup:
  • Import Moment.js: Use const moment = require('moment'); to include the library.
  • Retrieve Local Time: javascript let date = moment().tz($env['GENERIC_TIMEZONE']);
  • Extract Date Components: javascript 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();
  • Format Date: javascript let formatted = date.format('YYYY-MM-DD HH:mm:ss.SSS Z');
  • Return JSON Output: javascript return [ { json: { utc: date, year: year, month: month, day: day, hour: hour, minute: minute, second: second, millisecond: millisecond, formatted: formatted } } ];
  • Integrate with Other Nodes:
  • The output from the Function node can be utilized by subsequent nodes, such as OpenWeatherMap for weather data or Telegram for sending messages, incorporating the localized datetime as needed.

🤖 Why Use This Automation Workflow?

  • Accurate Time Handling: Ensures that date and time data align with your specific timezone settings.
  • Environment Integration: Utilizes environment variables for dynamic timezone management.
  • Enhanced Functionality: Leverages Moment.js for comprehensive date and time manipulation within n8n.

👨‍💻 Who is This Workflow For?

This workflow is ideal for developers and automation enthusiasts who need to incorporate localized date and time information into their n8n workflows. It is particularly useful for those managing workflows that operate across different time zones.

🎯 Use Cases

  1. Scheduling Notifications: Send messages or alerts based on the user’s local time.
  2. Data Logging: Record events with precise local timestamps for accurate tracking and analysis.
  3. Report Generation: Create reports that reflect the local date and time, ensuring relevance and clarity.

TL;DR

This workflow provides a straightforward method to obtain and utilize local date and time within an n8n Function node using Moment.js. By correctly setting up environment variables and leveraging Moment.js, you can enhance your automated workflows with precise and localized time data.

Help us find the best n8n templates

About

A curated directory of the best n8n templates for workflow automations.