如何在JavaScript中合并两个日期? [英] How to merge two dates in JavaScript?

查看:134
本文介绍了如何在JavaScript中合并两个日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个日期对象。我想从一个时间和另一个时间取一个日期,并将它们组合成一个新的日期对象。

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

#我想要结束
datetime.toString()= Wed Dec 21 2011 03:00:00 GMT + 0000(GMT)

如何最好地完成这个?

解决方案

  var datetime = new Date(date.getFullYear(),date.getMonth(),date.getDate(),
time.getHours(),time.getMinutes(),time.getSeconds());


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?

解决方案

var datetime = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 
               time.getHours(), time.getMinutes(), time.getSeconds());

这篇关于如何在JavaScript中合并两个日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆