views:

4598

answers:

8

I'd like to get a Date object which is 30 minutes later to another Date object. How to achieve it?

Basically:

Tue Jul 28 16:00:00 UTC+0800 2009 add 30 min to Tue Jul 28 16:30:00 UTC+0800 2009

Tue Jul 28 16:40:00 UTC+0800 2009 add 30 min to Tue Jul 28 17:10:00 UTC+0800 2009

+10  A: 
var newDateObj = new Date();
newDateObj.setTime(oldDateObj.getTime() + (30 * 60 * 1000));
chaos
+2  A: 

Maybe something like this?


var d = new Date();
var v = new Date();
v.setMinutes(d.getMinutes()+30);

Chacha102
@Chacha102: You don't need two `Date` objects. `var d = new Date(); d.setMinutes(d.getMinutes() + 30);`
Grant Wagner
He wanted two date objects. Read the Question. One Date object which is 30 minutes ahead of another date object.
Chacha102
+16  A: 
var d1 = new Date (),
    d2 = new Date ( d1 );
d2.setMinutes ( d1.getMinutes() + 30 );
alert ( d2 );
Jamie
@Jamie: You don't need two `Date` objects. `var d = new Date(); d.setMinutes(d.getMinutes() + 30);`
Grant Wagner
@Grant: I assumed d2 = "I'd like to get a Date object" and d1 = "to another Date object"
Jamie
+1  A: 

I built a little calendar popup script in js, its on Github, maybe look through the code to see how the date object is interected with. Also, check Javascript Kit as its an awesome js reference, especially for the date object.

Christian
+1  A: 

This could also be done in one line:

var newDateObj = new Date(oldDateObj.getTime() + diff*60000);

(Where diff is the difference in minutes you want from oldDateObj's time.)

Kip
A: 

Just another option, which I wrote:

DP_DateExtensions Library

It's overkill if this is all the date processing that you need, but it will do what you want.

Supports date/time formatting, date math (add/subtract date parts), date compare, date parsing, etc. It's liberally open sourced.

Jim Davis
A: 

var now = new Date(); now.setMinutes(now.getMinutes() + 30);

Teo Graca
A: 

If current time is 6:25, Its writing the value as "6:1284600312322"

Vijay Prasath