Temporal.PlainDate.from()
Temporal.PlainDate.from()
静态方法从另一个 Temporal.PlainDate
对象、具有日期属性的对象或 RFC 9557 字符串创建一个新的 Temporal.PlainDate
对象。
语法
Temporal.PlainDate.from(info)
Temporal.PlainDate.from(info, options)
参数
info
-
以下之一:
-
一个
Temporal.PlainDate
实例,它会创建该实例的一个副本。 -
一个
Temporal.PlainDateTime
实例,它以与Temporal.PlainDateTime.prototype.toPlainDate()
相同的方式提供日历日期。 -
一个
Temporal.ZonedDateTime
实例,它以与Temporal.ZonedDateTime.prototype.toPlainDate()
相同的方式提供日历日期。 -
一个包含日期和可选日历的 RFC 9557 字符串。
-
一个包含以下属性的对象(按检索和验证的顺序)
calendar
可选-
一个与
calendarId
属性对应的字符串。有关常用支持的日历类型列表,请参阅Intl.supportedValuesOf()
。默认为"iso8601"
。所有其他属性都以该日历系统解释(与Temporal.PlainDate()
构造函数不同,后者在 ISO 日历系统中解释这些值)。 日
-
一个与
day
属性对应的整数。无论overflow
选项如何,都必须是正数。 era
和eraYear
-
一个字符串和一个整数,它们与
era
和eraYear
属性对应。仅当日历系统具有纪元时才使用。era
和eraYear
必须同时提供。必须提供eraYear
(与era
一起)或year
中的至少一个。如果同时提供了era
、eraYear
和year
,则它们必须一致。 月份
-
与
month
属性对应。无论overflow
选项如何,都必须是正数。必须提供month
或monthCode
中的至少一个。如果同时提供了month
和monthCode
,则它们必须一致。 monthCode
-
与
monthCode
属性对应。必须提供month
或monthCode
中的至少一个。如果同时提供了month
和monthCode
,则它们必须一致。 年
-
与
year
属性对应。必须提供eraYear
(与era
一起)或year
中的至少一个。如果同时提供了era
、eraYear
和year
,则它们必须一致。
信息应明确指定年份(作为
year
或era
和eraYear
)、月份(作为month
或monthCode
)和日期。
-
options
可选-
包含以下属性的对象
overflow
可选-
一个字符串,指定当日期组件超出范围时(使用对象
info
时)的行为。可能的值有:"constrain"
(默认)-
日期组件被限制在有效范围内。
"reject"
-
如果日期组件超出范围,则抛出
RangeError
。
返回值
一个新的 Temporal.PlainDate
对象,表示 info
在指定 calendar
中指定的日期。
异常
TypeError
-
在以下情况之一中抛出
info
不是对象或字符串。options
不是对象或undefined
。- 提供的属性不足以明确确定日期。您通常需要提供
year
(或era
和eraYear
)、month
(或monthCode
)和day
。
RangeError
-
在以下情况之一中抛出
- 指定相同组件的提供的属性不一致。
- 提供的非数字属性无效;例如,如果
monthCode
在此日历中从未是有效的月份代码。 - 提供的数字属性超出范围,并且
options.overflow
设置为"reject"
。 - 该信息不在 可表示范围 内,该范围是距 Unix 纪元 ±(108 + 1) 天,或大约 ±273,972.6 年。
示例
从对象创建 PlainDate
// Year, month, and day
const d1 = Temporal.PlainDate.from({ year: 2021, month: 7, day: 1 });
console.log(d1.toString()); // "2021-07-01"
// Year, month code, and day
const d2 = Temporal.PlainDate.from({ year: 2021, monthCode: "M07", day: 1 });
console.log(d2.toString()); // "2021-07-01"
// Year, month, day in a different calendar
const d3 = Temporal.PlainDate.from({
year: 2021,
month: 7,
day: 1,
calendar: "chinese",
});
// Note: when you construct a date with an object, the date components
// are in *that* calendar, not the ISO calendar. However, toString() always
// outputs the date in the ISO calendar. For example, the year "2021" in
// the Chinese calendar is actually 616 BC in the ISO calendar.
console.log(d3.toString()); // "-000616-08-12[u-ca=chinese]"
// Era, eraYear, month, and day
const d4 = Temporal.PlainDate.from({
era: "meiji",
eraYear: 4,
month: 7,
day: 1,
calendar: "japanese",
});
console.log(d4.toString()); // "1871-07-01[u-ca=japanese]"
控制溢出行为
默认情况下,超出范围的值会被钳制到有效范围
const d1 = Temporal.PlainDate.from({ year: 2021, month: 13, day: 1 });
console.log(d1.toString()); // "2021-12-01"
const d2 = Temporal.PlainDate.from({ year: 2021, month: 2, day: 29 });
console.log(d2.toString()); // "2021-02-28"
const d3 = Temporal.PlainDate.from("2021-02-29");
console.log(d3.toString()); // "2021-02-28"
你可以将此行为更改为抛出错误
const d3 = Temporal.PlainDate.from(
{ year: 2021, month: 13, day: 1 },
{ overflow: "reject" },
);
// RangeError: date value "month" not in 1..12: 13
从字符串创建 PlainDate
const d = Temporal.PlainDate.from("2021-07-01");
console.log(d.toLocaleString("en-US", { dateStyle: "full" }));
// Thursday, July 1, 2021
// Providing a calendar
const d2 = Temporal.PlainDate.from("2021-07-01[u-ca=japanese]");
console.log(
d2.toLocaleString("ja-JP", { calendar: "japanese", dateStyle: "full" }),
);
// 令和3年7月1日木曜日
// Providing a time and an offset (ignored)
const d3 = Temporal.PlainDate.from("2021-07-01T00:00+08:00");
console.log(d3.toString()); // "2021-07-01"
从另一个 Temporal 实例创建 PlainDate
const dt = Temporal.PlainDateTime.from("2021-07-01T12:00");
const d = Temporal.PlainDate.from(dt);
console.log(d.toString()); // "2021-07-01"
const zdt = Temporal.ZonedDateTime.from(
"2021-07-01T00:00+08:00[Asia/Shanghai]",
);
const d2 = Temporal.PlainDate.from(zdt);
console.log(d2.toString()); // "2021-07-01"
const d3 = Temporal.PlainDate.from(d);
console.log(d3.toString()); // "2021-07-01"
规范
规范 |
---|
Temporal # sec-temporal.plaindate.from |
浏览器兼容性
加载中…