How do I add 2 date/times together?
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 Dat开发者_开发问答e();
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;
}
else
{
return true;
}
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.");
}
else{
alert("Please book now");
}
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);
- 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.
精彩评论