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 );
         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

 I’m back… »

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 [...]

Comment from mmhan
Time: January 20, 2009, 9:33 pm

been looking around for this. Thanks.

Comment from Tina
Time: February 20, 2009, 3:29 am

Thanks for the code for a long time it looked and finally found it.

Comment from Johanna
Time: April 5, 2009, 11:42 am

Thank you for the methods for adding and subtracting time, and for the code for a long time.
The fact that I was looking for.

Comment from Rich
Time: April 9, 2009, 2:39 am

Should line 17: “value *= nanoseconds in days” be there? Is it meant to be a comment?

Comment from Rich
Time: April 9, 2009, 2:43 am

Oh, and missing semi-colons on lines: 10, 47!

Comment from spudly
Time: April 9, 2009, 6:08 am

Rich – Thanks for noticing the error on line 17 that should be a comment (although a bad one at that because it doesn’t accurately describe what is happening). I will remove the line completely. As for the semicolons, I generally try to put semicolons after every line in JavaScript. They are, however, optional (a line break has the same effect), so I think it’s a little nit-picky to point that out. The only circumstance in which that might break the code is if you attempt to use a JavaScript obfuscator of some kind, such as packer or shrinksafe. Either way, I’ll fix it. Thanks for your help.

Comment from Doris
Time: June 18, 2009, 10:28 am

Do not know about these methods to tochnostyu to add or subtract time from the facility. It is a pity that the month of such precision is not possible. Nevertheless, thanks for the info, it helped me.

Comment from tothda
Time: August 5, 2009, 8:36 am

Hi! Truncating to days should be this.setHours(0,0,0,0) instead of this.setMinutes(0,0,0,0)

Write a comment