我从数据库中获取以下日期
"2013-11-07 10:41:00"
看似只能在Chrome,而不是Safari或Firefox中解析。
//首先,从数据库中提取时间戳并将其放入数组
$getActivityUpdates1 = mysql_query("SELECT s.id, u.name, u.profilePic, s.userid, s.content, s.time1, s.imageKey FROM status_updates s
INNER JOIN users1 u ON u.id = s.userid
WHERE competitionId = '$competitionId' AND s.id > '$lastPollId' ORDER BY s.id DESC LIMIT 0, 20");
$results = array('items' => array());
while ($row = mysql_fetch_array($getActivityUpdates1))
{
$results['items'][] = array(
'statusid' => $row['id'],
'name' => $row['name'],
'profilePic' => $row['profilePic'],
'content' => $row['content'],
'time1' => $row['time1'],
'imageKey' => $row['imageKey'],
);
if ($lastPollId < $row["id"]) {
$lastPollId = $row["id"];
}
}
$results["lastPollId"] = $lastPollId;
die(json_encode($results));
// sparta.js - 这里我有一个将时间戳转换为“漂亮日期”的基本功能
function getNiceDate(d) {
var date = new Date(d).add(-new Date().getTimezoneOffset()).minutes(); //Problem with the format here?
var now = new Date(); //Problem with the format here?
var minutesDifference = parseInt((now - date) / (60 * 1000));
if (minutesDifference < 2) {
return "Just now";
} else if (minutesDifference < 60) {
return minutesDifference + " minutes ago";
} else if (minutesDifference < 60 * 24) {
var hoursDifference = parseInt(minutesDifference / 60);
return hoursDifference + " hours ago";
}
return date.toString("yyyy-MM-dd HH:mm"); //Problem with the format here?
}
//然后我们在构建HTML时调用getNiceDate()函数。 //entryData.time1是来自DB的时间戳,格式为“2013-11-07 10:41:00”
var activityTimeElement = entry.find(".actTime");
activityTimeElement.text(getNiceDate(entryData.time1)); //function getNiceDate() used here.
activityTimeElement.attr("data-timestamp", entryData.time1);
//输出和预期结果
In chrome : "35 minutes ago" or "Just now" or if more than a few hours ago, the timestamp.
In Safari : "NaN-NaN-NaN NaN:NaN"
In Firefox : "NaN-NaN-NaN NaN:NaN"
答案 0 :(得分:3)
检查
或
IETF-compliant RFC 2822 timestamps
根据这些,您的日期时间字符串必须按照以下格式进行格式化:
1995-02-05T00:00:00
此链接也可能对您有所帮助:
JavaScript new Date() Returning NaN in IE or Invalid Date in Safari