P
ROTOTYPE
I'm Not Only The Prototype, I'm Also A Member.
home
▪
stats
▪
search
▪
linkback
▪
about
▪
FAQ
| user: guest,
login
,
register
Date
:
Date Math enhancement
author:
layer51
[+]
,
Submitted: 05.26.03 8a
//------------------------------------------------------ // ::: DATE OBJECT ENHANCEMENT // Created by: Luke Bayes // Please send any comments, corrections or modifications // to : luke@lukebayes.com // Otherwise you are free to use this code for any reason // in any application. // When using addTime or removeTime the UNIT argument // will be used to identify which unit of time to // add to the current date object - // it should be one of the following string values: // // ms == milliseconds // ss == seconds // mi == minutes // hh == hours // dd == days // wk == weeks // mm == months // yyyy == years // // VAL will be the numeric measurement to add //------------------------------------------------------ // EXAMPLE USAGE: // var myDay = new Date(); // myDay.addTime("dd", 2); // trace("myDay : " + myDay); // myDay.addTime("yyyy", 2); // trace("myDay : " + myDay); // myDay.removeTime("mm", 3); // trace("myDay : " + myDay); //------------------------------------------------------ // ::: PUBLIC DATE METHODS BELOW - //------------------------------------------------------ // Return the difference between the current date object // and the one passed in as dateRef. Return value is in the // unit specified. (any supported above)... // will Round results by default, pass boolean true in doNotRound // argument to return complex numbers. Date.prototype.getTimeDiff = function(unit, dateRef, doNotRound) { if(!(dateRef instanceof Date)) return if(!this.customInited) this.customInit(); var oms = dateRef.getTime(); var mms = this.getTime(); var diff = (mms - oms < 0) ? oms - mms : mms - oms; return (doNotRound) ? diff / this.unitValues[unit] : Math.round(diff / this.unitValues[unit]); } //------------------------------------------------------ // Will add the amount of time specified to the current date // object in the units passed in. See above for supported // units. Date.prototype.addTime = function(unit, amt) { if(isNaN(amt)) return else var amt = Number(amt); if(!this.customInited) this.customInit(); if(unit == "mm") { this.setMonth( this.getMonth() + amt ); } else if(unit == "yyyy") { this.setFullYear( this.getFullYear() + amt ); } else { var actual = this.unitValues[unit] * amt; this.setTime( this.getTime() + actual ); } } //------------------------------------------------------ // Will remove time from the current date object in the // units specified. See above for supported units. Date.prototype.removeTime = function(unit, amt) { this.addTime(unit, -amt); } //------------------------------------------------------ // ::: PRIVATE HELPER METHODS BELOW - //------------------------------------------------------ Date.prototype.getUnitsInMilliseconds = function() { var obj = new Object(); obj.ms = 1; obj.ss = 1000; obj.mi = obj.ss * 60; obj.hh = obj.mi * 60; obj.dd = obj.hh * 24; obj.wk = obj.dd * 7; obj.mm = obj.dd * 30; obj.yyyy = obj.dd * 365; return obj; } //------------------------------------------------------ Date.prototype.customInit = function() { this.unitValues = this.getUnitsInMilliseconds(); this.customInited = true; } //------------------------------------------------------ trace(">> Date Loaded");
usage
trace("date start : " + myDate); var myDate = new Date(); myDate.addTime("mm", 3); trace("date finish : " + myDate);
msg
1
{
Truthtob
[+]
, posted: 10.06.06 11a•-, top
[^]
}
the "unitValues[unit]" array is not included in this script so the functions don't work.
Unless this is a hidden array within the date object that I don't know about...which is very possible.
Add Comment
[+]
›opyleft 2001-2010. Layer51 is: Jaime Prado.
@