1

我从数据库中获取以下日期

"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?    
}

//然后我们像这样调用 getNiceDate() 函数,因为我们构建了 HTML。//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"
4

1 回答 1

3

查看

ISO8601

或者

符合 IETF 的 RFC 2822 时间戳

根据这些,您的日期时间字符串必须按照以下格式设置:

1995-02-05T00:00:00

此链接也可能对您有所帮助:

JavaScript new Date() 在 IE 中返回 NaN 或在 Safari 中返回无效日期

于 2013-11-07T09:51:25.000 回答