Strange behavior of moments.js [duplicate]
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?
javascript momentjs
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.
add a comment |
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?
javascript momentjs
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 formoment(Object)
) are 0 indexed, so you are a creating a moment object that represents 2018-12-09
– VincenzoC
Nov 22 '18 at 8:21
add a comment |
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?
javascript momentjs
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
javascript momentjs
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 formoment(Object)
) are 0 indexed, so you are a creating a moment object that represents 2018-12-09
– VincenzoC
Nov 22 '18 at 8:21
add a comment |
1
Months in JavaScript (and formoment(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
add a comment |
1 Answer
1
active
oldest
votes
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
Documentation about Month input
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>
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
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
Documentation about Month input
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>
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
add a comment |
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
Documentation about Month input
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>
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
add a comment |
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
Documentation about Month input
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>
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
Documentation about Month input
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>
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
add a comment |
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
add a comment |
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