39

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

2
  • 1
    can you addin a small code snippet or jsfiddle that shows an example with actual and expected values? Commented May 16, 2013 at 21:39
  • If you follow the link to the datepicker and timepicker, there are actual working samples of both. Commented May 16, 2013 at 21:40

8 Answers 8

34

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

Sign up to request clarification or add additional context in comments.

2 Comments

Even if I set the format for the datepicker (like data-date-format="dd. MM yyyy") the value set to the scope is a date, not a string. I managed to get what I wanted with the following code: Date.parse($scope.date.toString("dd.MM.yyyy") + " " + $scope.fromTime);
This may not work in Safari. I tried running new Date('2018-10-30 10:00') in Safari 12 and got Invalid Date back. Safari can parse new Date('2018-10-30T10:00') but that will produce a different result than other browsers (see my comment to Jan.J's answer).
19

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.

2 Comments

moment.js is a lifesaver
Be careful with this. I ran new Date('2018-10-30T10:00') in Safari and got Tue Oct 30 2018 03:00:00 GMT-0700 (PDT). But I ran the same thing in Chrome and got Tue Oct 30 2018 10:00:00 GMT-0700 (Pacific Daylight Time). It seems that Safari interprets the time as UTC, and most other browsers interpret it as whatever the browser's time zone is.
8

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;
};

2 Comments

What does the '' + in front of year in the row where you concatinate the dateString do? Since it doesn't contain a space, it shouldn't do anything.
@shaedrich seems you are right it doesn't need that at least in a quick test on playcode.io/new. I'm guessing at the time it was to force string concatenation, seems it may not have been necessary.
6

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();

Comments

3

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

1 Comment

You can now use padStart() to simplify your solution like that: time.getMinutes().toString().padStart(2, '0')
1

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;
};

1 Comment

Are you using ampm somewhere else in the code? It would only make sense if you also convert a 24 hours based hour to a 12 hours based hour.
1

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');

Comments

0
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.