How to format BC dates (like “-700-01-01”)?

纵饮孤独 提交于 2020-02-23 10:13:26

问题


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?


回答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.




回答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.



来源:https://stackoverflow.com/questions/25846123/how-to-format-bc-dates-like-700-01-01

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!