How to combine date and time using dayjs

javascript

To combine a date and time using Day.js, you can use the .set() method to set the time part of the date to the desired time value. Here is an example:

const dayjs = require('dayjs');

// create a date object with a specific date
const date = dayjs('2023-04-23');

// create a time object with a specific time
const time = dayjs('10:30:00', 'HH:mm:ss');

// combine the date and time objects
const dateTime = date.set('hour', time.hour())
                    .set('minute', time.minute())
                    .set('second', time.second());

console.log(dateTime.format('YYYY-MM-DD HH:mm:ss')); // Output: 2023-04-23 10:30:00

In this example, we first create a date object with a specific date using the Day.js constructor. Then, we create a time object with a specific time using the HH:mm:ss format string to parse the time string.

Next, we use the set() method on the date object to set the hour, minute, and second values to the corresponding values from the time object. Finally, we format the resulting dateTime object using the YYYY-MM-DD HH:mm:ss format string and log it to the console.

Note that in this example, we assume that both the date and time objects are in the same timezone. If the date and time objects are in different timezones, you may need to convert them to a common timezone before combining them.