Time Zone Conversion with Moment.js in Next.js

Installation

npm install moment

Time Zone Conversion

// Original date in GMT
const originalDate = moment.utc('2024-04-22 12:00:00');

// Convert to a different time zone (e.g., New York)
const convertedDate = originalDate.tz('America/New_York');

console.log(convertedDate.format('YYYY-MM-DD HH:mm:ss')); // Output: 2024-04-22 08:00:00

Input Format

When specifying time zones in Moment.js, it’s essential to adhere to the city format recommended by the IANA Time Zone Database. This format includes the continent/city structure, such as ‘America/Newyork’ or ‘Europe/London’.

Daylight Saving Time (DST)

Moment.js excels at handling daylight saving time (DST) seamlessly, ensuring accurate time zone conversions even during transitions. It automatically detects DST shifts and adjusts the converted times accordingly.

Leave a comment

Your email address will not be published. Required fields are marked *