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

Share Improve this question edited Oct 12, 2017 at 13:43 Samer Abu Gahgah 7411 gold badge9 silver badges18 bronze badges asked Oct 12, 2017 at 13:39 sanitizersanitizer 1053 silver badges14 bronze badges 2
  • 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
Add a ment  | 

3 Answers 3

Reset to default 7

Date.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