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...
To combine date and time column into a timestamp, you can use cast() function with concat(). select cast(concat(yourDateColumnName, ' ', yourTimeColumnName) as datetime) as anyVariableName from yourTableName; In the above concept, you will use cast() when your date and time is in string format.
Use the Date() constructor to convert a string to a Date object, e.g. const date = new Date('2022-09-24') . The Date() constructor takes a valid date string as a parameter and returns a Date object. Copied! We used the Date() constructor to convert a string to a Date object.
The date and time is broken up and printed in a way that we can understand as humans. JavaScript, however, understands the date based on a timestamp derived from Unix time, which is a value consisting of the number of milliseconds that have passed since midnight on January 1st, 1970.
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 the constructor param
Combine date and time to string like this:
1997-07-16T19:20:15
Then you can parse it like this:
Date.parse('1997-07-16T19:20:15');
You could also use moment.js or something similar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With