The Expanded-Year Date Format is detailed in the ECMA-262 Language Specifications paragraph 21.4.1.15.1 Expanded Years
Quote:
21.4.1.15.1 Expanded Years
Covering the full-time value range of approximately 273,790 years forward or backwards from 1 January 1970 (21.4.1.1) requires representing years before 0 or after 9999. ISO 8601 permits expansion of the year representation.
In the simplified ECMAScript format, such an expanded year representation shall have 6 digits and is always prefixed with a + or - sign. The year 0 is considered positive and hence prefixed with a + sign.
NOTE Examples of date-time values with expanded years:
-271821-04-20T00:00:00Z ===> 271822 B.C.
-000001-01-01T00:00:00Z ===> 2 B.C.
+000000-01-01T00:00:00Z ===> 1 B.C.
+000001-01-01T00:00:00Z ===> 1 A.D.
+001970-01-01T00:00:00Z ===> 1970 A.D.
+002009-12-15T00:00:00Z ===> 2009 A.D.
+275760-09-13T00:00:00Z ===> 275760 A.D.
I have tried the following Javascript date methods to convert a (Year, Month, Day) to the corresponding Expanded-Year Date format:
const date = new Date(30,11,20);
console.log(date.toString()); // "Sat Dec 20 1930 00:00:00 GMT+0400 (Arabian Standard Time)"
console.log(date.toISOString()); // "1930-12-19T20:00:00.000Z"
console.log(date.toDateString()); // "Sat Dec 20 1930"
console.log(date.toGMTString()); // "Fri, 19 Dec 1930 20:00:00 GMT"
console.log(date.toUTCString()); // "Fri, 19 Dec 1930 20:00:00 GMT"
console.log(date.toLocaleDateString()); // "12/20/1930"
All of the above Date() Methods cannot convert a (Year, Month, Day) into the 'Expanded-Year Date' format. They also convert any year between 0 and 99 to the years 1900 to 1999.
As a possible solution, I have generated the following one-liner code as an attempt to find a way to do it:
const expandedDate=(Y, M, D)=>(Y<0?"-":"+")+("00000"+Math.abs(Y)).slice(-6)+"-"+("0"+M).slice(-2)+"-"+("0"+D).slice(-2);
However, I believe this approach of converting dates to strings and then slicing strings into parts is not what I would call the best approach.
Is there a better/simpler method either as a Javascript Built-In Method or another way to convert a (Year, Month, Day) format to the corresponding Expanded-Year Date format?
const expandedDate=(Y, M, D)=>(Y<0?"-":"+")+("00000"+Math.abs(Y)).slice(-6)+"-"+("0"+M).slice(-2)+"-"+("0"+D).slice(-2);
console.log(expandedDate(30,11,20));
console.log(expandedDate(2022,11,20));
console.log(expandedDate(-30,11,20));
console.log(expandedDate(-3200,11,20));
console.log(expandedDate(0,11,20));
console.log(expandedDate(-271821,4,20));