spudly.shuoink.com

the best way to predict the future is to implement it

Entries Comments


Date.add(), Date.subtract(), & Date.truncate()

4 July, 2008 (07:36) | JavaScript

If you’ve ever worked extensively with dates in javascript, you may have missed methods like these. These methods allow you to add or subtract time from a Date object. Truncate forces your date object into a specified precision. Here’s some examples:

var d = new Date();
//=> Fri Jul 04 2008 07:48:53 GMT-0600 (MDT)
d.add( "years", 1 );
//=> Fri Jul 04 2009 07:48:53 GMT-0600 (MDT)
d.subtract( "minutes", 38 );
//=> Fri Jul 04 2009 07:10:53 GMT-0600 (MDT)
d.truncate( "minute" );
//=> Fri Jul 04 2009 07:10:00 GMT-0600 (MDT)
d.truncate( "day" );
//=> Fri Jul 04 2009 00:00:00 GMT-0600 (MDT)
d.truncate( "year" );
//=> Thu Jan 01 2009 00:00:00 GMT-0600 (MDT)

Beware, however, of adding months. Because months have variable lengths, it’s difficult to know what should happen if you’re on the tail-end of a longer month. My script has the same behavior as native JavaScript, that is, if you are on day 31 of a given month, and the next month has less than 31 days then when you add 1 month to it, you’ll end up with the first day of the third month.

Here’s the code:

Date.prototype.add = function( /**String*/unit, /**Number*/value ) {

   unit = unit.replace( /s$/ ).toLowerCase();

   switch ( unit ) {
      case "year":
         this.setYear( this.getYear() + value );
         break;
      case "month":
         this.setMonth( this.getMonth() + value )
         break;
      case "week":
         this.setTime( this.getTime() + value * 604800000 );
         break;
      case "day":
         this.setTime( this.getTime() + value * 86400000 );
         value *= nanoseconds in days
         break;
      case "hour":
         this.setTime( this.getTime() + value * 3600000 );
         break;
      case "minute":
         this.setTime( this.getTime() + value * 60000 );
         break;
      case "second":
         this.setTime( this.getTime() + value * 1000 );
         break;
      case "nanosecond":
         // Fall Through
      default:
         this.setTime( this.getTime() + value );
         break;
   }

   return this;
};

Date.prototype.subtract = function( /**String*/unit, /**Number*/value ) {

   unit = unit.replace( /s$/ ).toLowerCase();

   switch ( unit ) {
      case "year":
         this.setYear( this.getYear() - value );
         break;
      case "month":
         this.setMonth( this.getMonth() - value )
         break;
      case "week":
         this.setTime( this.getTime() - value * 604800000 );
         break;
      case "day":
         this.setTime( this.getTime() - value * 86400000 );
         break;
      case "hour":
         this.setTime( this.getTime() - value * 3600000 );
         break;
      case "minute":
         this.setTime( this.getTime() - value * 60000 );
         break;
      case "second":
         this.setTime( this.getTime() - value * 1000 );
         break;
      case "nanosecond":
         // Fall Through
      default:
         this.setTime( this.getTime() - value );
         break;
   }
};

Date.prototype.truncate = function( /**String*/to ) {

   unit = unit.replace( /s$/ ).toLowerCase();

   switch ( unit ) {
      case "year":
         this.setMonth( 0, 1 );
         this.setHours( 0, 0, 0, 0 );
         break;
      case "month":
         this.setDate( 1 );
         this.setHours( 0, 0, 0, 0 );
         break;
      case "week":
         this.subtract( "day", this.getDay() );
         break;
      case "day":
         this.setMinutes( 0, 0, 0, 0 );
         break;
      case "hour":
         this.setMinutes( 0, 0, 0 );
         break;
      case "minute":
         this.setSeconds( 0, 0 );
         break;
      case "second":
         this.setMilliseconds( 0 );
         break;
      default:
         break;
   }

   return this;
};

« assert for javascript

 

Comments

Pingback from links for 2008-10-03 | xanders blog
Time: October 3, 2008, 10:04 pm

[...] spudly.shuoink.com ยป Date.add(), Date.subtract(), & Date.truncate() spudly.shuoink.com - the best way to predict the future is to implement it (tags: javascript) « links for 2008-10-03 [...]

Write a comment