gpt4 book ai didi

javascript - 如何在 jquery 上更改 dateTime json 的格式?

转载 作者:行者123 更新时间:2023-11-29 23:29:35 24 4
gpt4 key购买 nike

现在当我从数据库中显示日期和时间时,它看起来像这样:

Tue Dec 12 2017 02:03:00 GMT+0000 (Eastern Daylight Time) 4min. Description:duntik2

我还需要表演时间和(时间 + 持续时间)。像那样:

02:03 - 02:07

我喜欢这样的输出

Tue Dec 12 2017 02:03 - 02:07 Description: duntik2

这是我的 jquery ajax 代码

$.ajax({
url: url,
type: 'GET',
dataType: 'json',
success: function (data) {
var ul= $("#list_of_appointments");
for (i=0; i<data.length; i++){
var date = new Date(data[i].date_time);
var li= "<li><span class='date_time'>" + date + " </span>" +
"<span class='duration' Duration >" + data[i].duration + "min.</span>" +
"<span class='description'>" + data[i].description +
" </span>" + "<input type='hidden' value=" + data[i].id + " /></li>";
ul.append(li);
}});

那么你能帮我改一下这段代码吗?

最佳答案

您可以使用 toLocaleDateStringtoLocaleTimeString方法。

var i = 0, data = [{date_time: new Date(), duration:4, description:'Duntik', id:'some-id'}];
var date = new Date(data[i].date_time),
enddate = new Date(date);

enddate.setMinutes(enddate.getMinutes() + data[i].duration); // create the enddate from where we will extract the ending time

var formattedDate = date.toLocaleDateString('en-US', {
weekday: 'short',
month: 'short',
day: 'numeric',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
hour12: false
}).replace(/,/g, ''),
formattedTime = enddate.toLocaleTimeString('en-US', {
hour: '2-digit',
minute: '2-digit',
hour12: false
});

var li = "<li><span class='date_time'>" + formattedDate + ' - ' + formattedTime + " </span>" +
"<span class='description'>" + data[i].description +
" </span>" + "<input type='hidden' value=" + data[i].id + " /></li>";


console.log(li);

关于javascript - 如何在 jquery 上更改 dateTime json 的格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47820695/

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