'How to format BC dates (like "-700-01-01")?
How to format ISO dates BC with Moment.js?
moment("-700-01-01").year(); // 700 (WRONG)
moment("-0700-01-01").year(); // 700 (WRONG)
moment("-000700-01-01").year(); // -700 (RIGHT)
For some reason a year notation with 6 digits works. Is that the "right" way? Why doesn't notation like "-700-01-01"
work?
Solution 1:[1]
This isn't a Moment.js-specific problem; the same happens if you attempt to initialise a Date()
object with the string you're using as well. If you create it as a Date()
object first and manually assign the year using setYear()
it does accept a date of -700
:
var date = new Date();
date.setYear(-700);
moment(date).year();
> -700
However as Niels Keurentjes has pointed out, date calculations this far back get quite complicated and may not be at all reliable.
If you want "-700-01-01" you can configure the year, month and day separately:
date.setYear(-700);
date.setMonth(0);
date.setDate(1);
console.log(date);
> Fri Jan 01 -700 11:53:57 GMT+0000 (GMT Standard Time)
As to whether the 1st day of the 1st month in 700BC was actually a Friday... you'll have to look that one up yourself.
Solution 2:[2]
in your example the minus sign is also used as separator between years, month and days. As you point out in the comment of the answer of James, using coma as separator helps to distinguish.
Solution 3:[3]
You can also
moment('0000-01-01', 'YYYY-MM-DD').set('y', -700)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Community |
Solution 2 | Michel Marro |
Solution 3 | Nibia |