Vegar Vegar - 6 months ago 47
Javascript Question

Combine date and time string into single date with javascript

I have a datepicker returning a date string, and a timepicker returning just a time string.

How should I combine those into a single javascript Date?

I thought I found a solution in Date.js. The examples shows an

at( )
-method, but I can't find it in the library...

Answer

You can configure your date picker to return format like YYYY-mm-dd (or any format that Date.parse supports) and you could build a string in timepicker like:

 var dateStringFromDP = '2013-05-16';

 $('#timepicker').timepicker().on('changeTime.timepicker', function(e) {
    var timeString = e.time.hour + ':' + e.time.minute + ':00';
    var dateObj = new Date(datestringFromDP + ' ' + timeString);
  });

javascript Date object takes a string as constructor parma