Take the 2-minute tour ×

I have to add the current time with another one.

For example, the time now is 10:30 on 21st July 2011.

I want to book a vehicle for today at 17:00, but 10:30 + $minHours < 17:00 which means that I cannot make the booking.

Here is my code:

var d = new Date();
var curr_day = d.getDay();
var curr_date = d.getDate();
var hours = d.getHours();
var minutes = d.getMinutes();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();

a = d_names[curr_day] + ', ' 
    + curr_date + ' ' 
    + m_names[curr_month] + ' ' 
    + curr_year;

t1 = document.getElementById('min_hr');

if(a == e.value) 
    alert("please call reservation to make a booking for collection in the next 24 hours");
    return false;
    return true;
share|improve this question
where you want to add the 2nd time and sould the 2nd one be the same time? –  Mujtaba Haider Jul 25 '11 at 8:37
Strictly speaking it does not make sense to add two times. What is 9:32am + 4:15pm? It does, however, make sense to add a time and a duration. What is 10:15am + 3.5 hours? Answer: 1:45pm. (Sorry fo r the lame U.S. twelve-hour timestamps.) What exactly are you trying to add? –  Ray Toal Jul 25 '11 at 8:42
add comment

3 Answers

up vote 1 down vote accepted

Try this.

d.setMinutes ( d.getMinutes() + 30 );

Check the properties of Date instance here Date - MDN Docs, especially the getters and setters there.

If you want to set the hours use

//An integer between 0 and 23, representing the hour
d.setHours ( d.getHours() + 2 );

The signature of the method is

setHours(hoursValue[, minutesValue[, secondsValue[, msValue]]])

Code for your exact requirement is this.

var $maxHour = 17; //5 PM
var $minHours = 3;
var $hourNow = new Date().getHours();
if( ( $minHours + $hourNow ) > $maxHour ){
    alert("Time is up.");
    alert("Please book now");
share|improve this answer
not minutes..hours –  Ashitha Jul 25 '11 at 8:40
have updated code –  naveen Jul 25 '11 at 8:57
add comment
  • have one date object .
  • Convert the time you want to add to it into number of minutes ( or seconds ) .
  • Set the minutes(seconds) of the date object as it's original value + the value computed in Step 2.
share|improve this answer
add comment

You just make a new object. Above you refer to var d = new Date(); writing d.getDay();.

Now you make a new object: var d2 = new Date(/* something in here */); and refer to it by d2.getDay();

Is that an answer to your question?

edited. Now you can add any hours to your date:

var d = new Date();
var d2 = new Date();
d2.setHours(d.getHours() + 2);
share|improve this answer
I have to add current time with another time –  Ashitha Jul 25 '11 at 8:39
I edited my answer –  avall Jul 25 '11 at 8:45
add comment

Your Answer


