Using the below time system, how would I set the current hour to a specific hour without changing any other fields (days, years, months, etc.)?
/* A method to change a date using intervals */
if (! setup.changeDate) {
setup.changeDate = function(date, interval, units) {
var d = new Date(date); // Don't change original date.
switch(interval.toLowerCase()) {
case 'years':
d.setFullYear(d.getFullYear() + units);
break;
case 'quarters':
d.setMonth(d.getMonth() + 3 * units);
break;
case 'months':
d.setMonth(d.getMonth() + units);
break;
case 'weeks':
d.setDate(d.getDate() + 7 * units);
break;
case 'days':
d.setDate(d.getDate() + units);
break;
case 'hours':
d.setTime(d.getTime() + units * 3600000);
break;
case 'minutes':
d.setTime(d.getTime() + units * 60000);
break;
case 'seconds':
d.setTime(d.getTime() + units * 1000);
break;
default:
break;
}
return d;
};
}
Comments
It is unclear if you're asking:
a. How to change the Hour represented by a Date ojbject:
You can use the Javascript <date>.setHours() function to do that.
b. How to change your changeDate function to support the assigning of a particular hour:
Personally I wouldn't do this because that function is designed to change a date based on a number of interval units and not to change an interval to a precise value, but if you really want to make that function do two different (semi-related) things then you could add non-plural time intervals to your switch statement. warning: You will need to start checking the value of the units parameter because functions like setHours are particular about the values passed to it.
Thanks for the info!
Using the worked!
Greyelf, you truly are the King of Kings!