擴展年份日期格式在 ECMA-262 語言規范第 21.4.1.15.1 擴展年份中詳細說明
報價:
21.4.1.15.1 擴展年
涵蓋從 1970 年 1 月 1 日 (21.4.1.1) 起向前或向后大約 273,790 年的全時值范圍,需要表示 0 之前或 9999 之后的年份。ISO 8601 允許擴展年份表示。
在簡化的 ECMAScript 格式中,這種擴展的年份表示應該有 6 位數字,并且總是以 或 - 號為前綴。0 年被認為是正數,因此以 號為前綴。
注意 具有擴展年份的日期時間值示例:
-271821-04-20 T00:00:00Z ===> 271822 BC
-000001-01-01 T00:00:00Z ===> 2 BC
000000-01-01 T00:00:00Z ===> 1 BC
000001-01-01 T00:00:00Z ===> 1 AD
001970-01-01 T00:00:00Z ===>公元1970年
002009-12-15 T00:00:00Z ===> 2009 AD
275760-09-13 T00:00:00Z ===> 275760 AD
我嘗試了以下 Javascript 日期方法將(年、月、日)轉換為相應的擴展年日期格式:
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"
以上所有 Date() 方法都不能將 (Year, Month, Day) 轉換為 ' Expanded-Year Date ' 格式。它們還將 0 到 99 之間的任何年份轉換為 1900 到 1999 年。
作為一種可能的解決方案,我生成了以下單行代碼,試圖找到一種方法:
const expandedDate=(Y, M, D)=>(Y<0?"-":" ") ("00000" Math.abs(Y)).slice(-6) "-" ("0" M).slice(-2) "-" ("0" D).slice(-2);
但是,我相信這種將日期轉換為字串然后將字串分割成部分的方法并不是我所說的最佳方法。
是否有更好/更簡單的方法作為 Javascript 內置方法或將(年、月、日)格式轉換為相應的擴展年日期格式的另一種方法?
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));
uj5u.com熱心網友回復:
比你的代碼行更多,更冗長。但可能更容易遵循。
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}));
uj5u.com熱心網友回復:
你的一行代碼是最好的選擇,它只能像這樣稍微改進:
const expandDate=(Y, M, D) => `${(Y<0?"-":" ")}${("00000" Math.abs(Y)).slice(-6)}-${("0" M).slice(-2)}-${("0" D).slice(-2)}`;
uj5u.com熱心網友回復:
這是一個遺留功能,建構式將 0 到 99 的年份值視為 1900 年,在實作Temporal物件之前無法修復(這可能在 ECMAScript 2023 中,因為它似乎迫在眉睫,但它不在最新的 ECMA-262 2022 中草案)。
至于格式化,ECMAScript 對日期的格式化支持非常有限,這就是為什么有這么多日期庫的原因。您可以組合現有的格式化方法或創建一個全新的格式化方法。
例如,如果只需要正數年,那么您可以利用toLocaleDateString:
console.log(
new Date()
.toLocaleDateString('en-CA')
.replace(/^(.\d )/,y => (y<0? '-' : ' ') ('' Math.abs(y))
.padStart(6,'0'))
);
但是,toLocale方法似乎不支持負年份,它們總是沒有符號。因此,如果需要負年度支持,則回到基礎:
// Format as ISO 8601 with extended years
function extendedYears(date = new Date()) {
let z = n => (n < 10? '0' : '') n;
let y = date.getFullYear();
return (y<0?'-':' ') ('' Math.abs(y)).padStart(6,'0')
'-' z(date.getMonth() 1) '-' z(date.getDate());
}
console.log(extendedYears());
console.log(extendedYears(new Date(-1,0)));
console.log(extendedYears(new Date(20203,0)));
但實際上,如果將其作為函式呼叫,那么“單線”并不是真正的目標:功能性、清晰、可維護和無錯誤的代碼才是真正的目標。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/429336.html
標籤:javascript 日期 约会时间
