0

I would like to save the datepicker date to MySQL, as I can able to select the date format using the javascript. Even I tried logging to console the format looks fine as YYYY-MM-DD. Unfortunate, when INSERT query to MySQL the datepicker from the input type picks up today's date.

PackingDate: new Date().toISOString().slice(0, 19).replace('T', ' ') i tried passing the req.PackingDate

Like PackingDate: req.PackingDate.toISOString().slice(0, 19).replace('T', ' ')

But nothing works as it keeps storing today's date.

ejs:

<input type="text" id="PackingDate" name="PackingDate"/>

javascript:

$(document).ready(function () {
    var date = new Date();
    var currentMonth = date.getMonth();
    var currentDate = date.getDate();
    var currentYear = date.getFullYear();

$('#PackingDate').datepicker({
    minDate: new Date(currentYear, currentMonth, currentDate),
    dateFormat: 'yy-mm-dd'
});

});

.js:

PackingDate: new Date().toISOString().slice(0, 19).replace('T', ' ')
Usman Maqbool
  • 3,351
  • 10
  • 31
  • 48
Rocket
  • 31
  • 10
  • This is a suggestion instead of an answer to your question, so instead of storing date as a string, i would suggest you to store a timestamp as an integer in MySQL, and you can reformat that timestamp on client side as you want. FOR MORE https://stackoverflow.com/questions/409286/should-i-use-the-datetime-or-timestamp-data-type-in-mysql?rq=1 – Gulam Hussain Jul 05 '19 at 11:26

1 Answers1

0

If you want to get the date when the user selects it, you can do this:

$("#PackingDate").datepicker({
    onSelect: function() { 
        var dateObject = $(this).datepicker('getDate'); 
    }
});
Kamran
  • 181
  • 3
  • 13
  • Thank you for suggestion. Will this date save to the database selected one not current date. – Rocket Jul 05 '19 at 11:10
  • So how do you pass the variable dateObject to js file ? – Rocket Jul 05 '19 at 11:24
  • I don't know what are you asking about. dateObject is js variable, How are you sending your rest of the data to database? – Kamran Jul 05 '19 at 11:32
  • Well now i got it right, but there is a format problem in the table as it shows in this format "Thu Jun 09 2011 00:00:00 " as i need YYYY-MM-DD format in the table <%= data[i].Date %> – Rocket Jul 05 '19 at 12:09