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:0
Solution 1:
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 integerbetween0and23, 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 PMvar$minHours = 3;
var$hourNow = new Date().getHours();
if( ( $minHours + $hourNow ) > $maxHour ){
alert("Time is up.");
}
else{
alert("Please book now");
}
Solution 2:
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 = newDate();
var d2 = newDate();
d2.setHours(d.getHours() + 2);
Solution 3:
- 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.
Post a Comment for "How Do I Add 2 Date/times Together?"