Lord Vermillion
Lord Vermillion

Reputation: 5424

Add 2hours to datetime-string in javascript

I get the following string from a webservice, 2014-06-05T10:27:47Z. I want to add 2hours to this.

I tried to convert it to a date and add the time, but it doesn't work. Code below:

var d = new Date("2014-06-05T10:27:47Z");
d = new Date(d + 2*60*60*1000);

What am i doing wrong?

Upvotes: 0

Views: 149

Answers (2)

vintastic
vintastic

Reputation: 745

Use the setHours and getHours methods of the Date object instead of trying to do it yourself.

var d = new Date("2014-06-05T10:27:47Z");

d.setHours(d.getHours() + 2)

Upvotes: 5

Georgi Bilyukov
Georgi Bilyukov

Reputation: 627

You can use setHours method:

var d = new Date("2014-06-05T10:27:47Z");
var d2 = new Date("2014-06-05T10:27:47Z");
d2.setHours ( d .getHours() + 2 );

Upvotes: 5

Related Questions