Inconsistent behavior of JavaScript Date() function in iOS [duplicate]

问题: This question already has an answer here: Why does Date.parse give incorrect results? 11 answers I use the Date() function to convert MySQL Date to...

问题:

This question already has an answer here:

I use the Date() function to convert MySQL Date to the JS Date object. Following is the code of how I was doing it:

var a = "2019-03-12 12:30:03"; //MySQL Date

function MySQLToDate(dateString) {
   if (typeof dateString === 'string') {
      return new Date(dateString);
   }
   else {
      return dateString; //in case the argument is already an object
   }
}

alert(MySQLToDate(a));
//iOS Output: Invalid Date
//Normal Output: Tue Mar 12 2019 12:30:03 GMT+0530 (India Standard Time)

It was working absolutely fine as expected until I tested it in iPad browsers.

It returns Invalid Date in iOS. And of course, using the prototype function of Date() like getDate(), getMonth() etc return Nan.

So to overcome this situation, I researched and in one of the answers, I found out that passing the MySQL date directly into the JS Date function is a wrong practice. [Source]

Now my code looks like following:

var a = "2019-03-12 12:30:03"; //MySQL Date

function MySQLToDate(dateString) {
   if (typeof dateString === 'string') {
      return new Date(dateString.replace(" ","T"));
   } 
   else {
       return dateString; //in case the argument is already an object
   }
}

alert(MySQLToDate(a));
//iOS Output: Tue Mar 12 2019 18:00:03 GMT+0530 (India Standard Time)
//Normal Output: Tue Mar 12 2019 12:30:03 GMT+0530 (India Standard Time)

This solution is not giving an Invalid Date Error but literally gives an Invalid Date.

I also tried splitting the MySQL date and passing year, month, date, hours, minutes, seconds into the Date() but the time in the object is still wrong.

Your answers are most welcome.


回答1:

You can split the string and then use the Date's constructor:

// Use this constructor
    new Date(year, month, day, hours, minutes, seconds, milliseconds)

// code goes like this
    let tokens = "2019-03-12 12:30:03".split(/[-: ]/g)
    let date = new Date(tokens[0], tokens[1], tokens[2], tokens[3], tokens[4], tokens[5]);
    console.log(date);

This solution is working in safari, chrome and firefox browser.


回答2:

The "invalid" time you're getting looks a lot like a "double" IST timezone date. IST = India Standard Time is 5.5 hours (5 hours 30 minutes) ahead of Greenwich Mean Time (GMT+5.5).

When parsing/replacing the 'T' in your date, make sure to add a 'Z' on the end, to indicate Zulu time. Zulu time is timezone independent (UTC).

return new Date(dateString.replace(" ","T") + "Z");

Not sure if the iPad is choosing a timezone by itself or not, but either way you're not specifying any Timezone, so you leave it open for it's own interpretation and liking.

  • 发表于 2019-03-16 08:49
  • 阅读 ( 194 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除