1

const input = '02/02/1700 12:30'
const inputFormat = ['DD/MM/YYYY HH:mm']
const displayFormat = 'YYYY-MM-DDTHH:mm:ss'


console.log(moment(input, inputFormat, true))
console.log(moment(input, inputFormat, true).format())
console.log(moment.utc(input, inputFormat, true))
console.log(moment.utc(input, inputFormat, true).format())
console.log(moment.parseZone(input, inputFormat, true))
console.log(moment.parseZone(input, inputFormat, true).format())
console.log(moment.tz(input, inputFormat, true, 'Asia/Hong_Kong'))
console.log(moment.tz(input, inputFormat, true, 'Asia/Hong_Kong').format())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.31/moment-timezone-with-data.min.js"></script>

https://jsfiddle.net/y9gtvbdn/

The above example the result of parseZone give 1700-02-02T12:29:18Z which is different from input '02/02/1700 12:30'. Anyone can explain this to me?

Morty Choi
  • 2,466
  • 1
  • 18
  • 26
  • Hi. Please don't just link to external code. You can use an external site to *supplement* your question, but you need to include the specific code you are asking about in the question itself. Please read [*How do I ask a good question?*](https://stackoverflow.com/help/how-to-ask) and [*How to create a Minimal, Reproducible Example*](https://stackoverflow.com/help/how-to-ask) from the Stack Overflow help center. Thanks. (Also, there's too much in your fiddle anyway - limit your code to the specific question - remove the parts that aren't relevant.) – Matt Johnson-Pint Jul 23 '20 at 17:21
  • Just want to show different cases. Anyway, I suspect this is a moment bug. – Morty Choi Jul 24 '20 at 04:43
  • Updated with the code anyway. – Morty Choi Jul 24 '20 at 04:45

1 Answers1

1

A few things:

  • Don't log a moment directly. Always use an output function like format
  • When you parse in Moment, internally Moment is calling setters on a Date object. Since you aren't passing seconds or milliseconds, the setter for that is never called. If you did have seconds, you'd see them set to zero as expected.
  • The reason that they aren't in this case is that there's a longstanding browser bug with Date objects constructed on or before 1883-11-18T12:03:57.999Z. See this comment, and the associated question. This isn't a moment bug, but a JavaScript browser quirk. This only matters for very early dates.
  • parseZone is specifically for use when the input has a time zone offset in it - either numeric like -07:00 or a Z (which indicates UTC). Your example input string has neither in it.
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575