Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Time System

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

  • Please use the code tag when posting your example - it's the C on the editor bar.

    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.
    var d = Date.now();
    
    d.setHours(10);
    


    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.
    case 'hour':
    	d.setHours(units);
    	break;
    
    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.
  • So, post like this?
    /* 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;
    };
    }
    

    Thanks for the info!

    Using the
    d.setHours(10);
    
    worked!

    Greyelf, you truly are the King of Kings!
Sign In or Register to comment.