我在 Safari 浏览器中格式化日期时遇到问题。请在下面找到详细信息:
value = "2018-02-06";
var tempDate = {value: new Date(value.toString().replace('-','/')) };
tempDate 在 Safari 中被设置为 null,但在其他浏览器(如 Chrome 和 IE)中则没有。你能告诉我这里有什么问题吗?
最佳答案
您只更换了一个-
,而不是全部。使用replace(/-/g,'/');
替换所有的-
substr (pattern) A String that is to be replaced by newSubStr. It is treated as a verbatim string and is not interpreted as a regular expression. Only the first occurrence will be replaced.
var value = "2018-02-06";
value = value.replace(/-/g,'/');
var tempDate = {value: new Date(value) };
console.log(tempDate);
你的代码只替换第一个-
,检查
value = "2018-02-06";
console.log(value.toString().replace('-','/'));
.toString()
将其转换为字符串。如果它已经是字符串,则无需使用它。
关于javascript - 在 Safari 中解析时日期显示为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48639802/