1

I have a database set up where landing time requires a datetime data type, and I am trying to pull this off using javascript.

The format looks like the following in the database:

0000-00-00 00:00:00

My best attempt have been the following:

$('#landing_time').val(function(){
      var d = new Date();
      return d.getFullYear() + "-" + d.getMonth() + "-" + d.getDate() + " " + d.getMinutes() + ":" + d.getSeconds() + ":" + d.getMilliseconds();
    });
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Jon220
  • 161
  • 1
  • 1
  • 8
  • getMonth returns 0..11 btw – Alex K. Oct 02 '15 at 12:50
  • First of all if take the date from JS it means from client it can be manuplated i recomended do it from server side `NOW()` in sql or in php `date(Y-m-d H:i:s)` if you still want to use JS your problem is in the H:i:s part H=Hour you are getting miniute m=minute : you are getting seconds i= seconds : you are getting miliseconds – Santa's helper Oct 02 '15 at 13:11
  • thanks could you kindly elaborate on the javascript part – Jon220 Oct 02 '15 at 13:47

1 Answers1

2

If you are simply wanting to save the time into the database you are better of letting MySQL handle the date by simply inserting NOW() into the database query.

However, you can find the answer to your question here: Format JavaScript Date to yyyy-mm-dd

Community
  • 1
  • 1