gpt4 book ai didi

javascript - IE 和 Chrome 的 ToLocaleDateString() 解决方法

转载 作者:行者123 更新时间:2023-12-03 18:18:26 25 4
gpt4 key购买 nike

我使用 JS 函数来声明一个 var 日期,该日期是该月下一个可用的第一天。即今天是 11/01/2017(mm/dd/yyyy)。该函数运行良好,但 IE 11+ 不会接受它,而是抛出一些额外的函数,然后表单将拒绝为无效日期,但在 Chrome 中它运行良好。

我已将代码更改为以下,适用于两种浏览器..

//Calculate and assign next available 1st day of the month
var date = new Date();
firstDay = new Date(date.getFullYear(), date.getMonth()+1, 1);
firstDay = (new Date(firstDay).toLocaleString('en-US').replace(/[^ -~]/g,''));

但是输出是:11/1/2017,%2012:00:00%20AM..。这在功能上是正确的,因为我的表单选择了 "11/1/2017"部分,并在 IE 和 Chrome 上忽略了其余部分,但是我怎样才能更改上面的代码,所以它只会删除“,%2012:00:00%20AM”?它只是为了整理传递用户数据的 URL

最佳答案

Date.toLocaleString接受 option参数允许声明你想要你的输出。

在您的情况下,您需要 {day: 'numeric', month:'numeric', year:'numeric'}:

var date = new Date();
firstDay = new Date(date.getFullYear(), date.getMonth() + 1, 1);
firstDay = new Date(firstDay).toLocaleString('en-US', {
day: 'numeric',
month: 'numeric',
year: 'numeric'
}).replace(/[^ -~]/g,'');
console.log(firstDay);
(这确实与不带选项的 Date.toLocaleDateString 相同)

关于javascript - IE 和 Chrome 的 ToLocaleDateString() 解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46711138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com