I decided to reply to my own message for the sake of saving “time.”
The DatePicker element only produces dates, not times. The TimePicker element will produce time strings. To parse the hour, minute, second, and milliseconds from the Value property of the element, you can use JavaScript’s .split() function to produce an array containing the hour, minute, and second of the time string. In the array element containing the seconds, you could simply discard everything after and including the decimal point. With that information now available, you could use the setHours, setMinutes, and setSeconds functions of JavaScript’s panoply of Date functions to set the hour, minute, and second of a particular date.
Here’s how to put it all together:
var myDate;
// NOTE: You should change the name of the elements below to the names used in your code.
myDate = $w(‘#datePicker1’).value;
// The value you get from the timePicker might be something like “10:47:25.357” meaning:
// 10 hours
// 47 minutes
// 25 seconds
// 357 milliseconds
var myTime = $w(‘#timePicker1’).value;
// Create an array from the timePicker’s value, which is of the type STRING.
var timeArr = myTime.split(‘:’);
// The above line, using my example, would produce an array that looks like this:
// timeArr[0] === 10
// timeArr[1] === 47
// timeArr[2] === 25.357
// Add an array element and split off the milliseconds into the new element.
timeArr.push(timeArr[2].split(‘.’)[1]);
// Store the seconds in the element formerly occupied by seconds.milliseconds
timeArr[2] = timeArr[2].split(‘.’)[0];
// Up to this point you are still working with strings, but the setHours() function requires integers.
// So, convert the string values from the split into integer values.
timeArr[0] = parseInt(timeArr[0], 10);
timeArr[1] = parseInt(timeArr[1], 10);
timeArr[2] = parseInt(timeArr[2], 10);
timeArr[3] = parseInt(timeArr[3], 10);
// Add the time elements to the date value in myDate.
myDate = myDate.setHours(timeArr[0], timeArr[1], timeArr[2], timeArr[3]);
// Finally, save it to your database’s date/time field.
All this is necessary if you want to PICK a date and time using the controls offered by Wix, but if you simply want to grab the actual current time, you can just do the following:
myDate = new Date();
In this case, myDate will hold the date and time.
In either case, you can then save the value of myDate in a Wix collection’s data/time field and end up with something that looks like a date and time.