大网数据广告活动
Web 标准与前端开发

HTMLInputElement: valueAsDate property

· 52CE 我爱测

The **`valueAsDate`** property of the HTMLInputElement interface represents the current value of the input element as a Date, or `null` if conversion is not possible.

HTML DOM

The valueAsDate property of the HTMLInputElement interface represents the current value of the input element as a Date, or null if conversion is not possible.

This property can also be set directly, for example to set a default date based on some condition. If the provided value is neither null nor a Date object, a TypeError is thrown. If the provided value is null or an invalid date, the input value is set to the empty string.

This property always returns null when accessed on an input that isn't date- or time-based. When setting this property on such an input, an InvalidStateError DOMException is thrown.

Value

A Date object or null if a conversion is impossible. The date returned should always be interpreted as a UTC time—for example, using methods like getUTCDate() instead of getDate(). If you are not careful, the result may be off by 1—for example, if the user lives in a negative UTC offset (the US, for example), then interpreting the date as a local date will result in the previous day from what the user selected.

The month, date, and week input types return a UTC date that represents the first instant of the inputted time span—that is, they are always midnight in UTC. For month, the date is the first day of the month. For week, the date is the Monday of the week. The time input type always has the date set to 1970-01-01.

The datetime-local input type does not support the valueAsDate property, because it represents a date and time in the local time zone (a wall clock time), but Date objects represent an absolute point in time. However, some browsers may provide a non-standard implementation. WHATWG is working on integrating the Temporal API with the date/time inputs to account for this use case.

Examples

Retrieving a date value

This example demonstrates accessing the valueAsDate property on an <input> of type input/week.

HTML

We include an <input> of type week:

<label for="date">Pick a date and time:</label>

<input name="date" id="date" type="week" />

<pre id="log"></pre>

JavaScript

When no date or time is selected, the empty input resolves to null. Each time a selection is made, a HTMLElement/change_event event is fired, updating the <pre> content showing the HTMLInputElement.value of the form control compared to that value as a date.

const logElement = document.getElementById("log");
const inputElement = document.getElementById("date");

logElement.innerText = `Initial value: ${inputElement.valueAsDate}`;

inputElement.addEventListener("change", () => {
  logElement.innerText = `${inputElement.value} resolves to ${inputElement.valueAsDate}`;
});
#log {
  height: 20px;
  padding: 0.5rem;
  background-color: #ededed;
}

Results

Retrieving a date value

Using Date methods

This example demonstrates applying Date methods directly to the valueAsDate property of an <input> of type input/date.

HTML

We include an <input> of type date:

<label for="date2">Pick a date:</label>

<input name="date2" id="date2" type="date" />

<pre id="log"></pre>

JavaScript

When no date is selected, the empty string resolves to null. Each time a selection is made, a HTMLElement/change_event event is fired. We then populate the log with the date selected, formatted using the Date object's Date.prototype.toLocaleDateString() method.

const logElement = document.getElementById("log");
const inputElement = document.getElementById("date2");
const options = {
  weekday: "long",
  year: "numeric",
  month: "long",
  day: "numeric",
};

logElement.innerText = `Initial value: ${inputElement.valueAsDate}`;

inputElement.addEventListener("change", () => {
  if (inputElement.valueAsDate !== null) {
    logElement.innerText = `You selected ${inputElement.valueAsDate.toLocaleDateString("en-US", options)}`;
  } else {
    logElement.innerText = `${inputElement.value} resolves to ${inputElement.valueAsDate}`;
  }
});
#log {
  height: 20px;
  padding: 0.5rem;
  background-color: #ededed;
}

Results

Using Date methods

The date may be a day off due to your local timezone.

Specifications

Browser compatibility

See also

  • input
  • HTMLInputElement.value
  • HTMLInputElement.valueAsNumber

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。