How to format BC dates (like "-700-01-01")?
Asked Answered
A

4

9

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?

Audacious answered 15/9, 2014 at 10:38 Comment(2)
As a general rule, you shouldn't want this anyway. Date calculations this long ago are going to be incredibly messy, and wrong, by definition, because the Gregorian calendar itself simply wasn't standardized back then. In a nutshell: there was never a 'January 1st' according to the calendar logic of Moment.js, in 700BC the only calendar sporadically being used was the Calendar of Numa which counted 355 days every year. The Gregorian calendar wasn't remotely reliable until 1752AD.Holzman
there are several reasons why to actually do this - maybe the user needs to present some data on a timeline made by "usuall" javascript tool, and thus this request is completly valid. No one doubts that dates BC are not reliable, when it comes to day of the week or so, but still there need to be way how to do it. World didnt start on 1.1.1970 ... :)Advance
H
7

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.

Hegumen answered 15/9, 2014 at 10:50 Comment(2)
Thanks, creating a date as new Date(-700, 0, 1) works indeed fine, but I need a serializable notation and ISO date is perfect for that. You can't really compare Moment.js with the default Date parser, moment.js offers a superset of features of Date and solves incompatibilities between browsers. So I would say this is a moment.js specific problem which could be solved by moment.js. Any clue on why 6 digit years do work?Audacious
To your question on why 6 year digits work: scholarslab.org/research-and-development/…Appetence
O
1

You can also

moment('0000-01-01', 'YYYY-MM-DD').set('y', -700)
Outworn answered 20/4, 2022 at 11:40 Comment(1)
I am using this idea in dayjs and it works quite nicely when you gotta deal with BC/BCE Dates or year. const getBCEYear = bce => dayjs().subtract(new Date().getFullYear() + bce, 'year')Castaneda
G
0

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.

Garrulity answered 15/9, 2014 at 13:4 Comment(1)
Definitely not! It doesn't work!Outworn
G
0

moment can display expanded years using the YYYYYY notation. This feature is recorded in the moment.js display documentation and elaborated upon in the ECMAscript documentation.

Glyptic answered 16/8, 2022 at 18:28 Comment(1)
That is interesting to know momentjs however is a legacy project and they encourage everyone to switch to more modern libraries instead: momentjs.com/docs/#/-project-statusAudacious

© 2022 - 2024 — McMap. All rights reserved.