I would like to get "2016-10-01" when I use moment("2016-09-31")
.
However, When I try it, I get INVALIDA DATE
message.
Is it possible?
I would like to get "2016-10-01" when I use moment("2016-09-31")
.
However, When I try it, I get INVALIDA DATE
message.
Is it possible?
Javascript Date
object does that automatically.
var str = "2016-09-31";
var d = new Date(str);
console.log(d.toDateString())
Logic:
.add(date, 'day')
var str = "2016-09-31";
var arr = str.split(/(?:-|\/)/);
var d = moment(arr[0] + "-" + arr[1]+"-1").add(+arr[2]-1, "day").format("DD-MM-YYYY");
console.log(d)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.0/moment.js"></script>
According to ECMA-262, when parsing an ISO 8601 format date string, if any part is out of bounds the result must be an invalid Date. It seems to me that moment.js is being consistent with the standard.
If you want to create dates from invalid parts, you should do it manually.
/* Parse a date in ISO 8601 format as local
** Allow invalid parts
** @param {string} s - string to parse
** @returns {Date}
*/
function parseInvalidDate(s) {
var b = s.split(/\D/);
return new Date(b[0], --b[1], b[2]);
}
console.log(parseInvalidDate('2016-09-31').toString())