1

Description of the Issue and Steps to Reproduce:

  1. Receive user input as 3/3 var response
  2. Parse into a date variable var date = moment(new Date(response))
  3. Doing a console.log of date gives moment("2001-03-03T00:00:00.000")

The year defaults to 2001. Since the user may input the date in their own format, I didn't want to add in a format as I wouldn't know what format they might want to enter.

After looking around, I found some Moment github issues on this (#635, #912) which mentioned that the issue was resolved, but I am still getting the default year of 2001.

I also found a suggestion to set the year as this year if left unspecified:

if (date.year() === 2001) {
    date.year() = moment().year();
}

This works, but feels like a dirty solution. Any ideas what I can do instead?

Thanks in advance!

Current Environment

  1. Node.js v8.9.4
  2. Moment.js v2.20.1
  3. VS code v1.19.3
  4. MS Bot SDK v3.14.0

p/s Still pretty new to the stackoverflow/ github issues, and not to sure where I should have posted instead. Please let me know if you need more information!

Muggle
  • 79
  • 9
  • Nothing to do with moment. `new Date('3/3')` returns it in 2001. – Daniel A. White Feb 01 '18 at 02:17
  • Hi, I added that because otherwise, moment would return me a depreciation warning, not unlike the one here (https://stackoverflow.com/questions/23263380/deprecation-warning-moment-construction-falls-back-to-js-date) Removing it also returns `moment("2001-03-03T00:00:00.000")` – Muggle Feb 01 '18 at 02:25
  • https://stackoverflow.com/questions/47388495/chrome-defaults-to-year-2001-when-initializing-a-date-instance-with-no-year-prov – MotKohn Feb 01 '18 at 03:16

1 Answers1

0

Use moment with a format string to ensure the format is proper. You might want to force the user to provide the year or use a date picker. The machine can only do so much

console.log(moment('3/3', 'MM/dd').toString());
// or
console.log(moment('3/3', 'dd/MM').toString());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.js"></script>
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445