CodexBloom - Programming Q&A Platform

implementing Date Formatting in JavaScript: Unexpected Output with Intl.DateTimeFormat

👀 Views: 36 💬 Answers: 1 📅 Created: 2025-06-14
javascript date intl JavaScript

I'm relatively new to this, so bear with me. I'm having trouble formatting dates using `Intl.DateTimeFormat` in JavaScript, particularly when trying to handle locales and options together. I'm currently working with the latest version of Node.js (v18.0.0) and need to format a date to a localized string that includes both the date and time with a specific timezone. However, I keep getting unexpected output which doesn't seem to respect the options I provide. For example, I’m using the following code: ```javascript const date = new Date('2023-10-10T15:00:00Z'); const options = { timeZone: 'America/New_York', year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric', hour12: true }; const formatter = new Intl.DateTimeFormat('en-US', options); const formattedDate = formatter.format(date); console.log(formattedDate); ``` I expect to see something like "October 10, 2023, 11:00 AM" but instead, I’m getting "October 10, 2023, 3:00 PM". I’ve verified that the UTC date is correct, but the output does not reflect the timezone adjustment I expected. I’ve double-checked the timezone setting and even tried using different locales, but the result remains the same. Is there something I’m missing in the configuration? Could this be an scenario with how Node.js handles the `Intl` API? Any insights would be appreciated! My development environment is Ubuntu. Any ideas what could be causing this? Could this be a known issue? This is happening in both development and production on Windows 10. I'd really appreciate any guidance on this.