admin管理员组文章数量:1289621
I was using a JS function to declare a var date that was the next available 1st day of the month. i.e. todays would be 11/01/2017(mm/dd/yyyy). The function was working perfectly however IE 11+ would not take it, and instead throw in some extra functions which the form would then reject as not being a valid date, however in Chrome it worked perfectly.
Ive changed the code to the following, which works on both browsers..
//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,''));
however the output is: 11/1/2017,%2012:00:00%20AM..
. which is functionally correct as my form picks up the "11/1/2017
" part, and ignores the rest, both on IE & Chrome, however how can I change the above code so it will just remove the ",%2012:00:00%20AM
"? Its just a sake of tidying up the URL which is passing along user-data
I was using a JS function to declare a var date that was the next available 1st day of the month. i.e. todays would be 11/01/2017(mm/dd/yyyy). The function was working perfectly however IE 11+ would not take it, and instead throw in some extra functions which the form would then reject as not being a valid date, however in Chrome it worked perfectly.
Ive changed the code to the following, which works on both browsers..
//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,''));
however the output is: 11/1/2017,%2012:00:00%20AM..
. which is functionally correct as my form picks up the "11/1/2017
" part, and ignores the rest, both on IE & Chrome, however how can I change the above code so it will just remove the ",%2012:00:00%20AM
"? Its just a sake of tidying up the URL which is passing along user-data
- I don't understand you just want to show 11/1/2017? If it is suppose to be used as a string does your variable firstDay has to be a Date for some reason? You could just write firstDay = date.getDate() + '/' + (date.getMonth() + 1) '/' + date.getFullYear(). Or you want something else? – Anastasios Selmani Commented Oct 12, 2017 at 13:53
- Or maybe even firstDay = (new Date(firstDay).toLocaleString('en-US')).Substring(0, 10); – Anastasios Selmani Commented Oct 12, 2017 at 13:58
3 Answers
Reset to default 7Date.toLocaleString
accepts an option parameter allowing to declare how you want your output.
In your case you'll want {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);
(Which will indeed be the same as Date.toLocaleDateString
without options)
You can use toLocaleDateString, this will give you just the date part in locale format.
var d = new Date();
console.log(d.toLocaleDateString("en-US"));
You could either use
firstDay = date.getDate() + '/' + (date.getMonth() + 1) '/' + date.getFullYear().
Or
firstDay = new Date(firstDay).toLocaleString('en-US')
本文标签: javascriptToLocaleDateString() workaround for IE amp ChromeStack Overflow
版权声明:本文标题:javascript - ToLocaleDateString() workaround for IE & Chrome - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741425058a2378029.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论