admin管理员组文章数量:1332352
I have a UTC string "2018-04-25T13:36:00"
which I pass into this function:
function convertUTCDateToLocalDate(dateString) {
var newDate = new Date(dateString);
newDate.setMinutes(dateString.getMinutes() - dateString.getTimezoneOffset());
return newDate;
}
var localDate = convertUTCDateToLocalDate(new Date("2018-04-25T13:36:00"));
console.log(localDate);
Chrome, Firefox and Edge returns correctly as (PASS):
Wed Apr 25 2018 06:36:00 GMT-0700 (PDT)
However, Safari returns it as (FAIL):
Tue Apr 24 2018 23:36:00 GMT-0700 (PDT)
Why is Safari being a stickler?
I have a UTC string "2018-04-25T13:36:00"
which I pass into this function:
function convertUTCDateToLocalDate(dateString) {
var newDate = new Date(dateString);
newDate.setMinutes(dateString.getMinutes() - dateString.getTimezoneOffset());
return newDate;
}
var localDate = convertUTCDateToLocalDate(new Date("2018-04-25T13:36:00"));
console.log(localDate);
Chrome, Firefox and Edge returns correctly as (PASS):
Wed Apr 25 2018 06:36:00 GMT-0700 (PDT)
However, Safari returns it as (FAIL):
Tue Apr 24 2018 23:36:00 GMT-0700 (PDT)
Why is Safari being a stickler?
Share Improve this question asked Apr 25, 2018 at 18:25 markreyesmarkreyes 1,2891 gold badge15 silver badges27 bronze badges 3- What is .getTimezoneOffset() giving you in safari vs everywhere else? – AndrewG Commented Apr 25, 2018 at 18:46
- I have Safari's web console open and plopped a console.log to see but I get nothing. Whereas other browsers are returning a value of 420. I can't connect the dots on this one. MDN says this method is supported in Safari: developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – markreyes Commented Apr 25, 2018 at 19:01
- "2018-04-25T13:36:00" isn't UTC, it should be parsed as local. So your function is actually treating a local date as UTC, not the other way around. And yes, there's a bug in Safari's parser. – RobG Commented Apr 26, 2018 at 0:33
1 Answer
Reset to default 6The string "2018-04-25T13:36:00" should be parsed as local, however Safari gets it wrong and parses it as UTC. Since you want to parse it as UTC, one simple (but not remended) method is to simply add "Z" to the string so all browsers parse it as UTC:
var s = '2018-04-25T13:36:00';
console.log(new Date(s+'Z').toString());
However, general advice is never use the built-in parser as it is notoriously problematic (see Why does Date.parse give incorrect results?), write your own parser for your particular format or use a library. To parse your format as UTC, consider:
var s = '2018-04-25T13:36:00';
// Parse YYYY-MM-DDTHH:mm:ss as UTC
function parseUTC(s) {
var b = s.split(/\D/);
return new Date(Date.UTC(b[0],--b[1],b[2],b[3],b[4],b[5]))
}
var d = parseUTC(s);
console.log(d.toISOString());
console.log(d.toString());
本文标签: javascriptUTC date conversion to Local date does NOT workSafariStack Overflow
版权声明:本文标题:javascript - UTC date conversion to Local date does NOT work, Safari - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742321736a2452927.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论