0

I am trying to get year, month and day from below code but getting wrong.

 import dateUtils from 'app/utils/date-utils';

 const today = dateUtils.momentToKey();

 const todayMoment = this.dateUtils.keyToMoment(today);
 const newYear = todayMoment.year();
 const newMonth = todayMoment.month();
 const newDay = todayMoment.date();

 console.log('newYear : ', newYear); 
 console.log('newMonth : ', newMonth); 
 console.log('newDay : ', newDay); 

Month showing wrong - enter image description here

Could you guide me what's wrong in code?

Anand Somani
  • 801
  • 1
  • 6
  • 15
  • 2
    For various reasons, months are indexed 0-11 (January: 0, December: 11); so you've got the right month even if - according to real-world dating conventions - it *looks* wrong. Effectively, in JavaScript, you just have to add 1 when displaying the month. I can't find an open question discussing this problem that has an accepted answer, but there's a few questions. Have a read of, for example: https://stackoverflow.com/questions/12254333/javascript-is-creating-date-wrong-month – David Thomas Dec 23 '22 at 12:05
  • 1
    Does this answer your question? [Zero-based month numbering](https://stackoverflow.com/questions/1453043/zero-based-month-numbering) – NullVoxPopuli Dec 27 '22 at 04:26

0 Answers0