gpt4 book ai didi

javascript - jQuery UI $.datepicker.formatDate 返回 NaN NaN Nan

转载 作者:行者123 更新时间:2023-12-02 16:05:39 37 4
gpt4 key购买 nike

我有一个 mySql 表,其中有一个名为 posts 的列,该列设置为 timestamp 类型,默认为 current_timestamp。然后,我使用 php PDO 获取它的值(以及其他一些列),并 echo json_encode 整个内容。我使用 AJAX 调用来调用我的 .php 文件,现在我想格式化我收到的日期。

一些谷歌搜索导致我为其 $.datepicker 函数添加了 jQuery-UI,作为格式化数据的简单方法。由于我可能会在页面上使用其他一些 jQuery-UI 元素,因此这似乎是一个合理的解决方案。到目前为止,我还无法让它工作。

当我console.log相关对象时,我得到这个结果“2015-06-10 13:26:54”

这是我的 JS 代码(您应该查看已发布的 var):

function makeArticleFeed(response) {
$('#articles-feed').empty();
if(response.length > 0) {
for(var x = 0; x<response.length; x++) {
console.log(response[x].posted);
var posted = $.datepicker.formatDate('dd m yy', new Date(response[x].posted));
$("#articles-feed").append(
'<div class="article-box"><h1><a href="#">'+response[x].title+
'</a></h1><h3><a href="#">' +response[x].author_id+
'</a>&nbsp;|&nbsp;'+ posted+
'</h3><p>'+response[x].extract+'</p></div>'
);
} //end article feed for loop
}
else {
$("#articles-feed").append('<h1>Sorry, no article found!</h1>');
}

最佳答案

这就是将字符串 Invalid Date 传递给 formatDate() 函数时得到的结果。

换句话说,response[x].posted 不是 javascript new Date 可以解析的有效日期。

如果它是来自 MySQL 的时间戳,则通常采用 2015-06-11 16:48:24 格式,并且您可以在大多数服务器端语言中获取纪元时间,例如只需将其传递给 PHP strtotime 可以工作,然后你会得到从纪元开始的秒数,并且 javascript 接受毫秒,因此你可以在服务器上或在将其传递给 new Date 之前乘以一千>.

关于javascript - jQuery UI $.datepicker.formatDate 返回 NaN NaN Nan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30787615/

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