The toDateString() method returns the date portion of a Date object in English in the following format separated by spaces:
First three letters of the week day name
First three letters of the month name
Two digit day of the month, padded on the left a zero if necessary
Four digit year (at least), padded on the left with zeros if necessary
E.g. “Thu Jan 01 1970”.
You have two easy options to convert your date to a proper string format:
1. Use some external Javascript library for date formatting like moment.js.
2. Or, in your case use some manual formatting:
As you only need a simple date format (yyyy-MM-dd), something as easy as the code below should do the job:
var date_input = new Date($("#date_input").val());
var day = date_input.getDay();
var month = date_input.getMonth() + 1;
var year = date_input.getFullYear();
var yyyy_MM_dd = year + "-" + month + "-" + day; // That's your formatted date.
Wrap it in a function to re-use it as needed:
function convertDateToString(date) {
let day = date.getDay();
let month = date.getMonth() + 1;
let year = date.getFullYear();
let yyyy_MM_dd = year + "-" + month + "-" + day; // That's your formatted date.
return yyyy_MM_dd;
}