Reputation: 105
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
Upvotes: 3
Views: 6587
Reputation: 24241
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"));
Upvotes: 2
Reputation: 137171
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);
Date.toLocaleDateString
without options)
Upvotes: 7
Reputation: 3689
You could either use
firstDay = date.getDate() + '/' + (date.getMonth() + 1) '/' + date.getFullYear().
Or
firstDay = new Date(firstDay).toLocaleString('en-US')
Upvotes: 1