Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 981
Jos de Jong Avatar asked Sep 15 '14 10:09

Jos de Jong


1 Answers

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.

like image 165
James Donnelly Avatar answered Oct 20 '22 07:10

James Donnelly