Vegar
Vegar

Reputation: 12898

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...

Upvotes: 39

Views: 91369

Answers (8)

x-magix
x-magix

Reputation: 2852

const date = "2022-12-27";
const time = "16:26:42";

new Date(`${date}T${time});

output

Date Tue Dec 27 2022 16:26:42 GMT+0100 (Central European Standard Time)

Upvotes: 0

Khushbu Raval
Khushbu Raval

Reputation: 1177

Concate date and time with moment JS which also works on firefox,

let d1 = moment().format('MM/DD/YYYY');
let dateTimeOpen = moment(d1 + ' ' + model.openingTime).format('YYYY-MM-DD HH:mm:ss');
let dateTimeClose = moment(d1 + ' ' + model.closingTime).format('YYYY-MM-DD HH:mm:ss');

Upvotes: 1

Kurt
Kurt

Reputation: 306

Boateng's example fails in cases where time consisted of hours, minutes, days or months that ranged from values 0-9 as getDate(), getMonth() etc... will return 1 digit in these cases and the time string will fail and an invalid date is returned:

function CombineDateAndTime(date, time) {
     const mins = ("0"+ time.getMinutes()).slice(-2);
     const hours = ("0"+ time.getHours()).slice(-2);
     const timeString = hours + ":" + mins + ":00";
     const year = date.getFullYear();
     const month = ("0" + (date.getMonth() + 1)).slice(-2);
     const day = ("0" + date.getDate()).slice(-2);
     const dateString = "" + year + "-" + month + "-" + day;
     const datec = dateString + "T" + timeString;
     return new Date(datec);
};

Unfortunately do not have enough rep to comment

Upvotes: 3

Jan.J
Jan.J

Reputation: 3080

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.

Upvotes: 19

dm03514
dm03514

Reputation: 55962

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

Upvotes: 34

mtpultz
mtpultz

Reputation: 18268

You can concatenate the date and time, and then use moment to get the datetime

const date = '2018-12-24';
const time = '23:59:59';

const dateTime = moment(`${date} ${time}`, 'YYYY-MM-DD HH:mm:ss').format();

Upvotes: 6

boateng
boateng

Reputation: 898

David's example with slight modifications:

function CombineDateAndTime(date, time) {
    var timeString = time.getHours() + ':' + time.getMinutes() + ':00';
    var ampm = time.getHours() >= 12 ? 'PM' : 'AM';
    var year = date.getFullYear();
    var month = date.getMonth() + 1; // Jan is 0, dec is 11
    var day = date.getDate();
    var dateString = '' + year + '-' + month + '-' + day;
    var datec = dateString + 'T' + timeString;
    var combined = new Date(datec);

    return combined;
};

Upvotes: 1

David Silva Smith
David Silva Smith

Reputation: 11716

For plain JavaScript:

combineDateAndTime = function(date, time) {
    timeString = time.getHours() + ':' + time.getMinutes() + ':00';

    var year = date.getFullYear();
    var month = date.getMonth() + 1; // Jan is 0, dec is 11
    var day = date.getDate();
    var dateString = '' + year + '-' + month + '-' + day;
    var combined = new Date(dateString + ' ' + timeString);

    return combined;
};

Upvotes: 8

Related Questions