moment / moment-timezone

Timezone support for moment.js
momentjs.com/timezone
MIT License
3.82k stars 835 forks source link

bug report: moment.tz format has some bug , unexpected year number 0002 #1115

Open Zola opened 1 month ago

Zola commented 1 month ago

Environment

For bug reports, please run the following code in your environment and include the output:

console.log({
  moment: moment.version,
  momentTz: moment.tz.version,
  date: (new Date()).toString(),
  intlZone: new Intl.DateTimeFormat().resolvedOptions().timeZone,
});

{moment: "2.29.1", momentTz: "0.5.34", date: "Sun Aug 04 2024 03:28:32 GMT+0800 (台北標準時間)", intlZone: "Asia/Taipei"}

Issue description

moment.tz('2:05 PM · Nov 12, 2010 ', ' h:mm A · MMM D, YYYY', 'America/New_York') , 0002-05-12 20:10:00 , should be 2010, not 0002, I think the code may have some bug to fix.

the moment.min.js are fine to format string moment('2:05 PM · Nov 12, 2010 ', ' MMMM D, YYYY h:mm A', true); 2010-11-12 14:05:00

same result with js from momentjs.com

<script src="https://momentjs.com/downloads/moment-timezone-with-data.js"></script>
gilmoreorless commented 1 month ago

I think you need to be careful with leading and trailing spaces in your format strings. When I run your examples in the browser console on https://momentjs.com, I don't see the year 0002, but I do see some weirdness. But removing the space at the start of the format strings makes everything work.

moment.tz('2:05 PM · Nov 12, 2010 ', ' h:mm A · MMM D, YYYY', 'America/New_York').format();
// "2024-08-05T12:20:00-04:00", completely wrong

// NOTE: No space at the start ------👇 
moment.tz('2:05 PM · Nov 12, 2010 ', 'h:mm A · MMM D, YYYY', 'America/New_York').format();
// "2010-11-12T14:05:00-05:00", correct