Welkom!

Deel je kennis met anderen in deze Flashgemeenschap!
Gebruikersnaam:
Wachtwoord:
Wachtwoord opslaan?
Wachtwoord vergeten?
Date.prototype.showDate
4393 keer gelezen
(July 26, 2003)
Waardering: 3 keer gestemd 
 3.7 van de 5
Een prototype om de datum, tijd, en alle andere datum-gerelateerde dingen op willekeurige opmaak en volgorde te laten zien.

Date.as is AS file met nodige proto's en een array.
In de AS daar onder staat alle AS van de showDate proto

De parameters zijn overgenomen uit PHP (http://nl.php.net/manual/nl/function.date.php)
(Hij is nog niet helemaal af, maar wilde hem voordat ik op vakantie ging alvast laten zien , tot over 10 dagen)

// --- Date.as ---

Date.dayArray = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
Date.monthArray = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "Oktober", "November", "December"];

Date.prototype.getDaysInMonth = function()
{
    return new Date(this.getFullYear(), this.getMonth()+1, 0).getDate();
}

Date.prototype.getFirstDayInMonth = function()
{
    return Date.dayArray[new Date(this.setDate(1)).getDay()];
}

/*
trace(new Date().getFirstDayInMonth()); // this month
trace(new Date(2003, 0).getFirstDayInMonth()); // month 0 (january)
trace(new Date(2004, 0).getFirstDayInMonth()); // month 0 (january) next year
*/


Date.prototype.getDayInYear = function()
{
    var firstYearDay = new Date(this.getFullYear(), 0);
    return Math.ceil((this - firstYearDay) / 86400000) + 1
}


Date.prototype.getWeekNr = function()
{
    var firstYearDay = new Date(this.getFullYear(), 0);
    var day = Math.ceil((this - firstYearDay) / 86400000) + 1
    var firstDay = firstYearDay.getDay();
    
    var days = firstDay + day - 3;
    var week = days/7 + 1;
    return Math.floor(week);
}


Date.prototype.isLeapyear = function()
{
    return (new Date(this.getFullYear(), 2, 0).getDate() - 28);
}



 

#include "Date.as"

Date.prototype.showDate = function(args)
{
     var dateString = this.toString().split(" ");
    
     var weekdayNr = this.getDay();
     var longDayName = Date.dayArray[weekdayNr];
     var shortDayName = longDayName.substr(0, 3);
     var shortDayNr = this.getDate();
     if ((shortDayNr == 1) || (shortDayNr == 11) || (shortDayNr == 21) || (shortDayNr == 31)) {
            daySuffix = "st";
     } else if ((shortDayNr == 2) || (shortDayNr == 12) || (shortDayNr == 22)) {
            daySuffix = "nd";
     } else if ((shortDayNr == 3) || (shortDayNr == 13) || (shortDayNr == 23)) {
            daySuffix = "rt";
     } else {
            daySuffix = "th";
        }
     var longDayNr = (shortDayNr < 10) ? ("0" + shortDayNr) : (shortDayNr);
    
     var monthNr = this.getMonth();
     var longMonthName = Date.monthArray[monthNr];
     var shortMonthName = longMonthName.substr(0, 3);
     var shortMonthNr = (monthNr + 1);
     var longMonthNr = (shortMonthNr < 10) ? ("0" + shortMonthNr) : (shortMonthNr);
     var totalMonthDays = monthArr[monthNr][1];
    
     var longYearNr = this.getFullYear();
     var shortYearNr = String(longYearNr).substr(-2);
    
     var timeArr = dateString[3].split(":");
     var sec = timeArr[2];
     var min = timeArr[1];
     var longHour24 = timeArr[0];
     var shortHour24 = this.getHours();
     var longHour12 = (longHour24 > 12) ? (longHour24 - 12) : (longHour24);
     if (longHour12 == 0) {
            longHour12 = 12;
     }
     var shortHour12 = Number (longHour12);
    
     var ampm = (hour < 12) ? "am" : " pm";
     var ampmH = ampm.toUpperCase();
    
     var timeOffsetHour = dateString[4].substr(3);
     var timeOffsetSec = this.getTimezoneOffset() * 60;
    
     var RFC822 = ((((((((((shortDayName + ", ") + shortDayNr) + " ") + shortMonthName) + " ") + longYearNr) + " ") + dateString[3]) + " ") + timeOffsetHour);
    
     var combi = [["a", ampm], ["A", ampmH], ["B", ""], ["d", longMonthNr], ["D", shortDayName],
                        ["F", longMonthName], ["g", shortHour12], ["G", shortHour24], ["h", longHour12], ["H", longHour24],
                        ["i", min], ["I", ""], ["j", shortDayNr], ["l", longDayName], ["L", this.isLeapyear()],
                        ["m", longMonthNr], ["M", shortMonthName], ["n", shortMonthNr], ["O", timeOffsetHour], ["r", RFC822],
                        ["s", sec], ["S", daySuffix], ["t", this.getDaysInMonth()], ["T", a], ["U", a],
                        ["w", weekdayNr], ["W", this.getWeekNr()], ["y", shortYearNr], ["Y", longYearNr], ["z", this.getDayInYear()], ["Z", timeOffsetSec]];
    
     var z = args.split("");
    
     for (j in z) for (i in combi) if(z[j].length == 1) z[j] = z[j].split(combi[i][0]).join(combi[i][1]);
    
     args = z.join("");
    
     return (args);
 };

 trace (new Date ().showDate("a-A-B-d-D"));
 trace (new Date ().showDate("F-g-G-h-H"));
 trace (new Date ().showDate("i-I-j-l-L"));
 trace (new Date ().showDate("m-M-n-O-r"));
 trace (new Date ().showDate("s-S-t-T-U"));
 trace (new Date ().showDate("w-W-y-Y-z-Z"));

Geef hier je waardering: Matig 1 2 3 4 5 Geweldig!
  Waardering:
Download ( meld je eerst aan om te kunnen downloaden! )