gpt4 book ai didi

javascript - 将当前时间注入(inject)日期脚本

转载 作者:行者123 更新时间:2023-11-30 13:55:49 25 4
gpt4 key购买 nike

我有一个用于设置预订日期的日期选择器。但是,输出只是给出 MM/DD/YYYY,但 REST API 需要 MM/DD/YYYYThh:mm:ssZ(老实说,不确定“T”和“Z”的用途,但文档就是这样写的) .

<script>
$(document).ready(function(){

var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();

today = mm + '/' + dd + '/' + yyyy;


$("#datepicker").datepicker({
beforeShowDay: nonWorkingDates,
numberOfMonths: 1,
minDate: today,
maxDate: '+2M',
firstDay: 0
});

function nonWorkingDates(date){
var day = date.getDay(), Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6;
var closedDates = [[7, 4, 2019]];
var closedDays = [[Sunday]];
for (var i = 0; i < closedDays.length; i++) {
if (day == closedDays[i][0]) {
return [false];
}

}

for (i = 0; i < closedDates.length; i++) {
if (date.getMonth() == closedDates[i][0] - 1 &&
date.getDate() == closedDates[i][1] &&
date.getFullYear() == closedDates[i][2]) {
return [false];
}
}

return [true];
}
});</script>

由于预订日期将按所选日期设置,所以时间对我来说似乎无关紧要,但代码似乎需要它。那么,有没有办法将时间戳注入(inject)日期变量,使其看起来像(使用示例)“2019-05-08T18:05:58.435Z”

最佳答案

使用日期方法toISOString()如下

var d = new Date();
d.toISOString() // "2019-08-03T05:06:47.249Z"

关于javascript - 将当前时间注入(inject)日期脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57335578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com