0

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.

3 Answers3

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));
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