user2656127
user2656127

Reputation: 665

Issues with javascript Date() in Safari and Firefox

I fetch the following date from the DB

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

Which seemingly can only be parsed in Chrome, NOT Safari or Firefox.

//Firstly, the timestamp is pulled from the DB and put into the array here

$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 - Here I have a basic function to convert the timestamp into a "pretty date"

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

//Then we call the getNiceDate() function like so, as we build the HTML. //entryData.time1 is the Timestamp from DB in following format "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);

//Output and Expected Results

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"

Upvotes: 1

Views: 2352

Answers (1)

Igl3
Igl3

Reputation: 5108

Check

ISO8601

or

IETF-compliant RFC 2822 timestamps

According to these your date-time string must be formatted according to this:

1995-02-05T00:00:00

This Link might help you too:

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

Upvotes: 3

Related Questions