javascript - Safari 和 Firefox 中的 javascript Date() 问题

标签 javascript jquery date datetime

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

"2013-11-07 10:41:00"

这似乎只能在 Chrome 中解析,不能在 Safari 或 Firefox 中解析。

//首先从DB中拉取时间戳,放入这里的数组中

$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 是来自数据库的时间戳,格式如下“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"

最佳答案

检查

ISO8601

IETF-compliant RFC 2822 timestamps

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

1995-02-05T00:00:00

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

JavaScript new Date() Returning NaN in IE or Invalid Date in Safari

关于javascript - Safari 和 Firefox 中的 javascript Date() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19832240/

相关文章:

javascript - Uncaught Error : Leaflet must be loaded before the leaflet heatmap plugin

JavaScript:为什么放在字符串末尾的反斜杠会破坏 JavaScript 代码?

javascript - onsubmit 验证所有函数返回真

java - 向日历添加天数

r - 如何在 R 中查找多个列中的最早日期(NAs 问题)

javascript - Google-apps-script 不会在 onEdit 函数内发送电子邮件

javascript - Coffeescript:数组元素匹配另一个数组

javascript - 将选择多个输入转换为 bool 数组

javascript - 如何克隆元素及其关联的事件回调?

python - 从文本 Python 中识别和提取日期的最佳方法?