首页 > 解决方案 > 如何使用 JQuery 或 Javascript 设置日期类型值的输入

问题描述

我的代码如下:

var date1 = new Date($("#date1").val() + "T00:00");
date1.setDate(date1.getDate() + 30);  // 30 days into the future

$date2 = $("#date2");  // input of type date
$date2.val(date1) 

除了最后一行之外,所有工作都按预期工作。错误说:

jquery-3.2.1.js:7990 The specified value "2021-07-02T03:00:00.000Z" does not conform to the required format, "yyyy-MM-dd

标签: javascriptjquery

解决方案


In case someone else has the same issue, this is how I solved it:

$date2.val(date1.toISOString().split("T")[0])

推荐阅读