user8909781
user8909781

Reputation:

JS: Safari new Date().getTime() gives different results from Chrome

I am doing:

new Date().getTime();

Chrome and Safari have different results.

How to make it work?

Plus, I want to get rid of this:

var n = new Date();
      var nYear = n.getFullYear();
      var nMonth = n.getMonth()+1;
      if (nMonth<10)
        nMonth = "0" + nMonth;
      var nDate = n.getDate();
      if (nDate<10)
        nDate = "0" + nDate;
      var date = nYear + "-" + nMonth + "-" + nDate;
      var nHours = n.getHours();
      if (nHours<10)
        nHours = "0" + nHours;
      var nMinutes = n.getMinutes();
      if (nMinutes<10)
        nMinutes = "0" + nMinutes;
      var startHour = nHours + ":" + nMinutes;

And turn it to something like this:

$date = date("Y-m-d");
$startHour = date("H:i");

Upvotes: 3

Views: 5549

Answers (2)

jlosada
jlosada

Reputation: 46

In this old post is the solution -> Invalid date in safari

After looking at datejs documentation, using it, your problem should be solved using code like this:

var myDate1 = Date.parseExact("29-11-2010", "dd-MM-yyyy");
var myDate2 = Date.parseExact("11-29-2010", "MM-dd-yyyy");
var myDate3 = Date.parseExact("2010-11-29", "yyyy-MM-dd");
var myDate4 = Date.parseExact("2010-29-11", "yyyy-dd-MM");

Upvotes: 0

Aravind S
Aravind S

Reputation: 2395

Safari Will be handling the date object some what differently from other browsers.When i face the issues, i tried something like in the code attached below and it worked well !

var dateSplit = data.event.start.date.split("-");
var date_instance = new Date(dateSplit[0], parseInt(dateSplit[1], 10) - 1, dateSplit[2]);

//where data.event.start.date  will be from a JSON like "2017-12-05"

Ref:Invalid date in safari and Safari Javascript Date() NaN Issue (yyyy-MM-dd HH:mm:ss)

Thanks !

Upvotes: 1

Related Questions