which one of these is a good use for a date/time value?

I guess I'll give an answer as well:Personally, I like to attempt to avoid gratuitous variable declaration, method calls, and constructor calls, as they are all expensive on perfor

which one of these is a good use for a date/time value?

I guess I'll give an answer as well:
Personally, I like to attempt to avoid gratuitous variable declaration, method calls, and constructor calls, as they are all expensive on performance. (within reason, of course)
I was going to leave this as just comment under the Answer given by @AnthonyWJones but thought better of it.// Prototype usage... Date.prototype.addDays = Date.prototype.addDays || function( days ) { return this.setTime( 864E5 * days + this.valueOf() ) && this; }; // Namespace usage... namespace.addDaysToDate = function( date, days ) { return date.setTime( 864E5 * days + date.valueOf() ) && date; }; // Basic Function declaration... function addDaysToDate( date, days ) { return date.setTime( 864E5 * days + date.valueOf() ) && date; };

The above will respect DST. Meaning if you add a number of days that cross DST, the displayed time (hour) will change to reflect that.
Example:
Nov 2, 2014 02:00 was the end of DST.var dt = new Date( 2014, 10, 1, 10, 30, 0 ); console.log( dt );                  // Sat Nov 01 2014 10:30:00 console.log( dt.addDays( 10 ) );    // Tue Nov 11 2014 09:30:00

If you're looking to retain the time across DST (so 10:30 will still be 10:30)...// Prototype usage... Date.prototype.addDays = Date.prototype.addDays || function( days ) { return this.setDate( this.getDate() + days ) && this; }; // Namespace usage... namespace.addDaysToDate = function( date, days ) { return date.setDate( date.getDate() + days ) && date; }; // Basic Function declaration... function addDaysToDate( date, days ) { return date.setDate( date.getDate() + days ) && date; };

So, now you have...var dt = new Date( 2014, 10, 1, 10, 30, 0 ); console.log( dt );                  // Sat Nov 01 2014 10:30:00 console.log( dt.addDays( 10 ) );    // Tue Nov 11 2014 10:30:00

Video liên quan