moment / moment-timezone

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

The parser may not ignore non-alphanumeric characters when it is with time zone. #1068

Closed worldwidepark closed 1 year ago

worldwidepark commented 1 year ago

Environment

system time zone - morocco. default time zone - japan.

doc says ↓ but, The parser may not ignore non-alphanumeric characters when it is with time zone. image

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

date format diffrence make difference of time zone. check code below

// system time zone - morocco.
// default time zone - japan. 
moment("2023/06/01 00:00:00").format() 
// result "2023-05-31T00:00:00+01:00"
// "2023/06/01" -> dose not follow default time zone. 
moment("2023-06-01 00:00:00").format()
// result "2023-05-31T00:00:00+09:00"
gilmoreorless commented 1 year ago

The part of the documentation you've quoted only applies when providing a format argument after the input, which is not what your code examples are doing. Calling moment(inputString) without a known format can have problems:

Warning: Browser support for parsing strings is inconsistent. Because there is no specification on which formats should be supported, what works in some browsers will not work in other browsers.

For consistent results parsing anything other than ISO 8601 strings, you should use String + Format.

When I try your first example I get a big warning in the console:

> moment("2023/06/01 00:00:00").format() 

Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.

Providing a format argument works as expected (e.g. moment("2023/06/01 00:00:00", "YYYY-MM-DD hh:mm:ss").format()).

Also, when a bug report template says "please run the following code in your environment and include the output", please actually follow the instructions. The debug info makes it much easier to diagnose problems quickly.