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));
CodePudding user response:
Way more lines of code and way more verbose than yours. But might be easier to follow.
function paddingZero(num, digit) {
const toAdd = digit - num.length;
let padding = "";
for (let index = 0; index < toAdd; index ) {
padding = "0";
}
return padding num;
}
function expandedDate({ year, month, day }) {
const yyyyyy = paddingZero(Math.abs(year).toString(), 6);
const mm = paddingZero(month.toString(), 2);
const dd = paddingZero(day.toString(), 2);
const prefix = year < 0 ? "-" : " ";
return `${prefix}${yyyyyy}-${mm}-${dd}`;
}
console.log(expandedDate({ year: 30, month: 11, day:20}));
console.log(expandedDate({ year: 2022, month: 11, day: 20}));
console.log(expandedDate({ year: -30, month: 11, day: 20}));
console.log(expandedDate({ year: -3200, month: 11, day: 20}));
console.log(expandedDate({ year: 0, month: 11, day: 20}));
console.log(expandedDate({ year: -271821, month: 4, day: 20}));
CodePudding user response:
Try this please:
const expandDate = (rawDate) => {
const [year1, year2] = rawDate.split('-');
const yearNumber = year1?.length ? parseInt(year1, 10) : parseInt(year2, 10) * -1;
const era = yearNumber > 0 ? 'A.D.' : 'B.C.';
const validYear = yearNumber > 0 ? Math.abs(yearNumber) : Math.abs(yearNumber) 1;
return `${validYear} ${era}`;
}
console.warn(expandDate('-271821-04-20T00:00:00Z')); // 271822 B.C.
console.warn(expandDate('-000001-01-01T00:00:00Z')); // 2 B.C.
console.warn(expandDate(' 000000-01-01T00:00:00Z')); // 1 B.C.
console.warn(expandDate(' 000001-01-01T00:00:00Z')); // 1 A.D.
console.warn(expandDate(' 001970-01-01T00:00:00Z')); // 1970 A.D.
console.warn(expandDate(' 002009-12-15T00:00:00Z')); // 2009 A.D.
console.warn(expandDate(' 275760-09-13T00:00:00Z')); // 275760 A.D.