Strange behavior of moments.js [duplicate]












-1
















This question already has an answer here:




  • Javascript Date.UTC() function is off by a month?

    3 answers







var now = moment({ year: 2018, month: 11, day: 9 }).format('M');
alert(now);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>





The expected behavior is alert shows '11' but you can see shows '12'



Someone know why?










share|improve this question













marked as duplicate by VincenzoC, jonrsharpe, adiga, Community Dec 4 '18 at 12:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    Months in JavaScript (and for moment(Object)) are 0 indexed, so you are a creating a moment object that represents 2018-12-09

    – VincenzoC
    Nov 22 '18 at 8:21
















-1
















This question already has an answer here:




  • Javascript Date.UTC() function is off by a month?

    3 answers







var now = moment({ year: 2018, month: 11, day: 9 }).format('M');
alert(now);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>





The expected behavior is alert shows '11' but you can see shows '12'



Someone know why?










share|improve this question













marked as duplicate by VincenzoC, jonrsharpe, adiga, Community Dec 4 '18 at 12:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    Months in JavaScript (and for moment(Object)) are 0 indexed, so you are a creating a moment object that represents 2018-12-09

    – VincenzoC
    Nov 22 '18 at 8:21














-1












-1








-1


0







This question already has an answer here:




  • Javascript Date.UTC() function is off by a month?

    3 answers







var now = moment({ year: 2018, month: 11, day: 9 }).format('M');
alert(now);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>





The expected behavior is alert shows '11' but you can see shows '12'



Someone know why?










share|improve this question















This question already has an answer here:




  • Javascript Date.UTC() function is off by a month?

    3 answers







var now = moment({ year: 2018, month: 11, day: 9 }).format('M');
alert(now);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>





The expected behavior is alert shows '11' but you can see shows '12'



Someone know why?





This question already has an answer here:




  • Javascript Date.UTC() function is off by a month?

    3 answers







var now = moment({ year: 2018, month: 11, day: 9 }).format('M');
alert(now);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>





var now = moment({ year: 2018, month: 11, day: 9 }).format('M');
alert(now);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>






javascript momentjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 8:17









Juan MedinaJuan Medina

6910




6910




marked as duplicate by VincenzoC, jonrsharpe, adiga, Community Dec 4 '18 at 12:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by VincenzoC, jonrsharpe, adiga, Community Dec 4 '18 at 12:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    Months in JavaScript (and for moment(Object)) are 0 indexed, so you are a creating a moment object that represents 2018-12-09

    – VincenzoC
    Nov 22 '18 at 8:21














  • 1





    Months in JavaScript (and for moment(Object)) are 0 indexed, so you are a creating a moment object that represents 2018-12-09

    – VincenzoC
    Nov 22 '18 at 8:21








1




1





Months in JavaScript (and for moment(Object)) are 0 indexed, so you are a creating a moment object that represents 2018-12-09

– VincenzoC
Nov 22 '18 at 8:21





Months in JavaScript (and for moment(Object)) are 0 indexed, so you are a creating a moment object that represents 2018-12-09

– VincenzoC
Nov 22 '18 at 8:21












1 Answer
1






active

oldest

votes


















3














The input of the month is from 0 to 11, when the output format is from 1 to 12.






var now = moment({ year: 2018, month: 0, day: 9 }).format('M');

console.log(now);

now = moment({ year: 2018, month: 11, day: 9 }).format('M');

console.log(now);

now = moment({ year: 2018, month: 12, day: 9 }).format('M');

console.log(now);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>







Documentation about M format



enter image description here



Documentation about Month input



enter image description here







To handle this case programmatically, you can either retrieve 1 to your month at the moment you set it or retrieve 1 at the end display.



Example :






var now = moment({ year: 2018, month: 11 - 1, day: 9 }).format('M');

console.log(now);


var now = String(Number(moment({ year: 2018, month: 11, day: 9 }).format('M')) - 1);

console.log(now);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>








share|improve this answer





















  • 1





    I see.... Thanks a lot.... but then what is the best way to have from var now = moment({ year: 2018, month: 11, day: 9 }) format to '2018-11-9'?

    – Juan Medina
    Nov 22 '18 at 10:57













  • @JuanMedina I've made an edit with the answer to your question

    – Grégory NEUT
    Nov 22 '18 at 11:01


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














The input of the month is from 0 to 11, when the output format is from 1 to 12.






var now = moment({ year: 2018, month: 0, day: 9 }).format('M');

console.log(now);

now = moment({ year: 2018, month: 11, day: 9 }).format('M');

console.log(now);

now = moment({ year: 2018, month: 12, day: 9 }).format('M');

console.log(now);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>







Documentation about M format



enter image description here



Documentation about Month input



enter image description here







To handle this case programmatically, you can either retrieve 1 to your month at the moment you set it or retrieve 1 at the end display.



Example :






var now = moment({ year: 2018, month: 11 - 1, day: 9 }).format('M');

console.log(now);


var now = String(Number(moment({ year: 2018, month: 11, day: 9 }).format('M')) - 1);

console.log(now);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>








share|improve this answer





















  • 1





    I see.... Thanks a lot.... but then what is the best way to have from var now = moment({ year: 2018, month: 11, day: 9 }) format to '2018-11-9'?

    – Juan Medina
    Nov 22 '18 at 10:57













  • @JuanMedina I've made an edit with the answer to your question

    – Grégory NEUT
    Nov 22 '18 at 11:01
















3














The input of the month is from 0 to 11, when the output format is from 1 to 12.






var now = moment({ year: 2018, month: 0, day: 9 }).format('M');

console.log(now);

now = moment({ year: 2018, month: 11, day: 9 }).format('M');

console.log(now);

now = moment({ year: 2018, month: 12, day: 9 }).format('M');

console.log(now);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>







Documentation about M format



enter image description here



Documentation about Month input



enter image description here







To handle this case programmatically, you can either retrieve 1 to your month at the moment you set it or retrieve 1 at the end display.



Example :






var now = moment({ year: 2018, month: 11 - 1, day: 9 }).format('M');

console.log(now);


var now = String(Number(moment({ year: 2018, month: 11, day: 9 }).format('M')) - 1);

console.log(now);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>








share|improve this answer





















  • 1





    I see.... Thanks a lot.... but then what is the best way to have from var now = moment({ year: 2018, month: 11, day: 9 }) format to '2018-11-9'?

    – Juan Medina
    Nov 22 '18 at 10:57













  • @JuanMedina I've made an edit with the answer to your question

    – Grégory NEUT
    Nov 22 '18 at 11:01














3












3








3







The input of the month is from 0 to 11, when the output format is from 1 to 12.






var now = moment({ year: 2018, month: 0, day: 9 }).format('M');

console.log(now);

now = moment({ year: 2018, month: 11, day: 9 }).format('M');

console.log(now);

now = moment({ year: 2018, month: 12, day: 9 }).format('M');

console.log(now);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>







Documentation about M format



enter image description here



Documentation about Month input



enter image description here







To handle this case programmatically, you can either retrieve 1 to your month at the moment you set it or retrieve 1 at the end display.



Example :






var now = moment({ year: 2018, month: 11 - 1, day: 9 }).format('M');

console.log(now);


var now = String(Number(moment({ year: 2018, month: 11, day: 9 }).format('M')) - 1);

console.log(now);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>








share|improve this answer















The input of the month is from 0 to 11, when the output format is from 1 to 12.






var now = moment({ year: 2018, month: 0, day: 9 }).format('M');

console.log(now);

now = moment({ year: 2018, month: 11, day: 9 }).format('M');

console.log(now);

now = moment({ year: 2018, month: 12, day: 9 }).format('M');

console.log(now);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>







Documentation about M format



enter image description here



Documentation about Month input



enter image description here







To handle this case programmatically, you can either retrieve 1 to your month at the moment you set it or retrieve 1 at the end display.



Example :






var now = moment({ year: 2018, month: 11 - 1, day: 9 }).format('M');

console.log(now);


var now = String(Number(moment({ year: 2018, month: 11, day: 9 }).format('M')) - 1);

console.log(now);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>








var now = moment({ year: 2018, month: 0, day: 9 }).format('M');

console.log(now);

now = moment({ year: 2018, month: 11, day: 9 }).format('M');

console.log(now);

now = moment({ year: 2018, month: 12, day: 9 }).format('M');

console.log(now);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>





var now = moment({ year: 2018, month: 0, day: 9 }).format('M');

console.log(now);

now = moment({ year: 2018, month: 11, day: 9 }).format('M');

console.log(now);

now = moment({ year: 2018, month: 12, day: 9 }).format('M');

console.log(now);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>





var now = moment({ year: 2018, month: 11 - 1, day: 9 }).format('M');

console.log(now);


var now = String(Number(moment({ year: 2018, month: 11, day: 9 }).format('M')) - 1);

console.log(now);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>





var now = moment({ year: 2018, month: 11 - 1, day: 9 }).format('M');

console.log(now);


var now = String(Number(moment({ year: 2018, month: 11, day: 9 }).format('M')) - 1);

console.log(now);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 11:01

























answered Nov 22 '18 at 8:22









Grégory NEUTGrégory NEUT

8,94321738




8,94321738








  • 1





    I see.... Thanks a lot.... but then what is the best way to have from var now = moment({ year: 2018, month: 11, day: 9 }) format to '2018-11-9'?

    – Juan Medina
    Nov 22 '18 at 10:57













  • @JuanMedina I've made an edit with the answer to your question

    – Grégory NEUT
    Nov 22 '18 at 11:01














  • 1





    I see.... Thanks a lot.... but then what is the best way to have from var now = moment({ year: 2018, month: 11, day: 9 }) format to '2018-11-9'?

    – Juan Medina
    Nov 22 '18 at 10:57













  • @JuanMedina I've made an edit with the answer to your question

    – Grégory NEUT
    Nov 22 '18 at 11:01








1




1





I see.... Thanks a lot.... but then what is the best way to have from var now = moment({ year: 2018, month: 11, day: 9 }) format to '2018-11-9'?

– Juan Medina
Nov 22 '18 at 10:57







I see.... Thanks a lot.... but then what is the best way to have from var now = moment({ year: 2018, month: 11, day: 9 }) format to '2018-11-9'?

– Juan Medina
Nov 22 '18 at 10:57















@JuanMedina I've made an edit with the answer to your question

– Grégory NEUT
Nov 22 '18 at 11:01





@JuanMedina I've made an edit with the answer to your question

– Grégory NEUT
Nov 22 '18 at 11:01



Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin