26

I have 2 date objects. I want to take the date from one and the time from the other and combine them into a new date object.

date.toString() = Wed Dec 21 2011 00:00:00 GMT+0000 (GMT)
time.toString() = Sun Dec 31 2000 03:00:00 GMT+0000 (GMT)

# I want to end up with
datetime.toString() = Wed Dec 21 2011 03:00:00 GMT+0000 (GMT)

How can I best accomplish this?

1
  • 2
    Do you need more information than this? Commented Dec 7, 2011 at 23:12

7 Answers 7

50
var datetime = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 
                        time.getHours(), time.getMinutes(), time.getSeconds());
Sign up to request clarification or add additional context in comments.

2 Comments

Note that getYear is deprecated and should be switch to getFullYear. Also, getDay returns the day of the week and should be switched to getDate.
As far as I get that, this solution does not account for daylight savings-time?
5

How about something like this:

var year  = date.getFullYear(),
    month = date.getMonth(),
    day   = date.getDate();

time.setFullYear(year);
time.setMonth(month);
time.setDate(day);

Comments

3
var parts = ['Hours', 'Minutes', 'Seconds', 'Milliseconds'];
for (var i=0, p; p=parts[i], i<parts.length; i++) {
  date['setUTC'+p]( time['getUTC'+p]() );
}

Comments

2

you can use string functions to build the target string...

datetime = date.toString().substr(0,16) + time.toString().substr(16,40)

then if you need to have it as a date object, feed it into a new date()

Comments

1
var msPerDay = 1000 * 60 * 60 * 24;
var newDateTime = new Date(date.getTime() + (time.getTime() % msPerDay));

EDIT: mine assumes date variable is a date without any time.

Comments

1

I don't know if this is the best, but you can try the following:

datetime = new Date(
  date.getUTCFullYear(),
  date.getUTCMonth(),
  date.getUTCDate(),
  time.getUTCHours(),
  time.getUTCMinutes(),
  time.getUTCSeconds()
);

For further reference on the Date object look here.

3 Comments

did not work for me - broke the TZ and messed up the time
@Kuf can you be a little more specific and maybe give an example? What exactly are you trying to accomplish? If it's very different from what the OP is asking you should consider starting / asking your own question.
I left this comment as a warning. in your code, you take local time, then convert it to UTC, then storing it again as local, which messes the TZ. I did resolve it by omitting the UTC from each function name (like @pna answer). hope that makes sense.
1

Here is a method using regexp to replace the time in date and then create the new Date object.

dateTime = new Date(date.toString().replace(/(\d\d:){2}\d{2}/,/(\d\d:){2}\d{2}/.exec(time.toString())[0]));

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.