The next in my Empowerment series of scripts tackles the Date object.
Rails introduced a ton of great shortcuts and methods to their Time
object, the equivalent to the Date object in Flash (though much easier
to use).  I've appropriated all their methods and added a few more.
There are some great methods in here that really save time when you need
to do things with the Date object.

--------------------------------------------------------------

// XDate
// The following calculations are all relative to the current date
object
// not the current date - this is an important distinction

// General function for easily altering the date object
// pass the option (a string) and how much you want to alter it
Date.prototype.change = function(option, amount) {
        if (isNan(Number(amount))) return undefined;
        var year = this.getFullYear();
        var time = this.getTime();
        // ms time diffs
        var aSec = 1000;
        var aMin = 60 * aSec;
        var anHour = 60 * aMin;
        var aDay = 24 * anHour;
        var aWeek = 7 * aDay;
        //
        switch (option) {
                case "years":
                        this.setFullYear(year + amount);
                        break;
                case "months":
                        this.setMonth(this.getMonth() + amount);
                        break;
                case "weeks":
                        this.setTime(time + (aWeek * amount));
                        break;
                case "days":
                        this.setTime(time + (aDay * amount));
                        break;
                case "hours":
                        this.setTime(time + (anHour * amount));
                        break;
                case "minutes":
                        this.setTime(time + (aMin * amount));
                        break;
                case "seconds":
                        this.setTime(time + (aSec * amount));
                        break;
                case "ms":
                        this.setTime(time + amount);
                        break;
                default:
                        trace("invalid option");
        }
};
// Sets date representing the start of the year (1st of january, 0:00) 
Date.prototype.beginningOfYear = function() {
        this.setMonth(0);
        this.setDate(0);
        this.midnight();
};
// Sets date representing the start of the quarter (1st of january,
april, july, october, 0:00)
Date.prototype.beginningOfQuarter = function() {
        var months = [0, 3, 6, 9];
        var n = 4;
        var m = this.getMonth();
        while (--n -(-1)) {
                if (months[n] <= m) {
                        this.setMonth(months[n]);
                        this.midnight();
                        break;
                }
        }
        
};
// Sets date representing the start of the month (1st of the month,
0:00) 
Date.prototype.beginningOfMonth = function() {
        this.setDate(1);
        this.midnight();
};
// Sets date representing the "start" of this week (default: Monday,
0:00)
// If you pass true, then it will set the "start" of the week to Sunday
Date.prototype.beginningOfWeek = function(sunday) {
        var today = this.getDay();
        var daysToSub = (today != 0 ? 1 - today : -6) - (sunday ? 1 :
0);
        this.change("days", daysToSub);
        this.midnight();
};
// Easy alias for beginningOfWeek()
Date.prototype.monday = function() {
        this.beginningOfWeek();
};
// Easy alias for beginningOfWeek(true)
Date.prototype.sunday = function() {
        this.beginningOfWeek(true);
};
// Sets date representing the start of the day (0:00) 
Date.prototype.midnight = function() {
        this.setHours(0);
        this.setMinutes(0);
        this.setSeconds(0);
        this.setMilliseconds(0);
};
// Sets date representing the end of the month (last day of the month,
0:00) 
Date.prototype.endOfMonth = function() {
        this.change("days", Math.abs(Math.floor((this - new
Date(this.getFullYear(), this.getMonth() + 1)) / (1000 * 60 * 60 * 24))
+ 1));
};
// Returns a new Date representing the time a number of specified years
ago 
Date.prototype.yearsAgo = function(years) {
        var d = new Date(this.getTime());
        d.change("years", this.getFullYear() - years);
        return d;
};
// Returns the number of years between the current date and the date
object
Date.prototype.yearsSince = function() {
        return new Date().getFullYear() - this.getFullYear();
};
// Returns a new Date representing the time a number of specified months
ago
Date.prototype.monthsAgo = function(months) {
        var d = new Date(this.getTime());
        d.change("months", this.getMonth() - months);
        return d;
};
// Returns the number of months between the current date and the date
object
Date.prototype.monthsSince = function() {
        var d = new Date();
        return ((d.getFullYear() - this.getFullYear()) * 12) +
(d.getMonth() - this.getMonth());
};
// Returns a new Date representing the time a number of specified days
ago
Date.prototype.daysAgo = function(days) {
        var d = new Date(this.getTime());
        d.change("days", days * -1);
        return d;
};
// Returns the number of days between the current date and the date
object
// The number of days between 12:01am Jan 2nd and 11:59pm Jan 1st = 1
Date.prototype.daysSince = function() {
        var d = new Date();
        var curTime = d.getTime();
        var thisTime = this.getTime();
        var aDay = 1000 * 60 * 60 * 24;
        var daysSince = Math.floor((curTime - thisTime) / aDay);
        var daysRemainder = ((curTime - thisTime) % aDay) / aDay;
        if (daysRemainder > 0 && (daysRemainder * aDay) >
(d.secondsSinceMidnight() * 1000)) daysSince++;
        return daysSince;
};
// Returns a new Date representing the time a number of specified hours
ago
Date.prototype.hoursAgo = function(hours) {
        var d = new Date(this.getTime());
        d.change("hours", hours * -1);
        return d;
};
// Returns the number of hours between the current date and the date
object
Date.prototype.hoursSince = function() {
        return (new Date().getTime() - this.getTime()) / (1000 * 60 *
60);
};
// Returns a new Date representing the time a number of specified
minutes ago
Date.prototype.minutesAgo = function(minutes) {
        var d = new Date(this.getTime());
        d.change("minutes", minutes * -1);
        return d;
};
// Returns the number of minutes between the current date and the date
object
Date.prototype.minutesSince = function() {
        return (new Date().getTime() - this.getTime()) / (1000 * 60);
};
// Returns a new Date representing the time a number of specified
seconds ago
Date.prototype.secondsAgo = function(seconds) {
        var d = new Date(this.getTime());
        d.change("seconds", seconds * -1);
        return d;
};
// Returns the number of seconds between the current date and the date
object
Date.prototype.secondsSince = function() {
        return (new Date().getTime() - this.getTime()) / 1000;
};
// Sets the date to next month
Date.prototype.nextMonth = function() {
        this.change("months", 1);
};
// Sets the date to last month
Date.prototype.lastMonth = function() {
        this.change("months", -1);
};
// Sets date representing the start of the given day in next week
(default is Monday). 
Date.prototype.nextWeek = function(sunday) {
        this.change("days", 7);
        if (!sunday) {
                this.monday();
        } else {
                this.sunday()
        }
};
// Sets date representing the start of the given day in last week
(default is Monday). 
Date.prototype.lastWeek = function(sunday) {
        this.change("days", -7);
        if (!sunday) {
                this.monday();
        } else {
                this.sunday()
        }
};
// Convenience method which sets date representing the time 1 day after
the instance date 
Date.prototype.yesterday = function() {
        this.change("days", -1);
};
// Convenience method which sets date representing the time 1 day since
the instance date 
Date.prototype.tomorrow = function() {
        this.change("days", 1);
};
// Returns the number of seconds since midnight of the instance date
Date.prototype.secondsSinceMidnight = function() {
        var d = new Date(this.getTime());
        d.midnight();
        return (this.getTime() - d.getTime()) / 1000;
};
_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to