I have a date 2015-7-15 and I want to add 9 hours to the given date I am trying but not getting result please help me.
Asked
Active
Viewed 1.1k times
0
-
3what did you try.... how did you get the above value – Arun P Johny Jul 15 '15 at 06:52
-
2possible duplicate of [Adding hours to Javascript Date object?](http://stackoverflow.com/questions/1050720/adding-hours-to-javascript-date-object) – Umesh Sehta Jul 15 '15 at 06:52
-
$(document).ready(function() { $(".start_date, .end_date").datepicker({dateFormat: 'yy-mm-dd'}); }); – Dipak-user3085702 Jul 15 '15 at 06:53
-
1This is a simple task which you should know how to research yourself. Even typing your question in to google gets you the answer. – Rory McCrossan Jul 15 '15 at 06:53
-
this is what I have done to get date value in the above format. – Dipak-user3085702 Jul 15 '15 at 06:54
3 Answers
3
You can get your answer by googling.
var myDate = new Date();
OR if you have the date, month and year, create a date as
var myDate = new Date(year, month, date);
then you can add the hours to your date
myDate.setHours(myDate.getHours()+h);
will do that

Marikkani Chelladurai
- 1,430
- 1
- 13
- 23
-
function setEndDate(k) { alert(k); var date2 = $('#startdate'+k).val(); //date2.setHours(date2.getHours()+15); date2.AddHours(24); alert(date2); $('#enddate'+k).val(date2); } – Dipak-user3085702 Jul 15 '15 at 06:56
-
What is k here? if k = 1, Do you have a field with ID = startdate1 ? – Marikkani Chelladurai Jul 15 '15 at 06:58
-
@Dipak-user3085702 you didn't convert `date2` to date object try this:`function setEndDate(k) { var date2 = new Date($('#startdate' + k).val()); date2.setHours(date2.getHours() + 15); alert(date2); $('#enddate' + k).val(date2); }` – Umesh Sehta Jul 15 '15 at 07:01
2
I created this function:
Date.prototype.addHours(h) {
this.setHours(this.getHours() + h);
return this;
}
and I called
alert(new Date().addHours(5));

Andrêves Dickow
- 160
- 7
1
Try this code
date.setMinutes(date.getMinutes()+ add 9 hour in minutes);
And I suggest to use time in UNIX time stamp.

Carl0s1z
- 4,683
- 7
- 32
- 47

Ankur Tiwari
- 2,762
- 2
- 23
- 40