Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Javascript: einen Datentyp um eine Funktion erweitern

26.01.2023 (👁2380)

Javascript: einen Datentyp um eine Funktion erweitern

Aufgabe: eine Datums-Variable nach der Kalenderwoche abfragen.

Hierzu muss der Datentyp Datetime um eine Function erweitert werden

var dt = new Date(col.dataField);

var calendarweek = dt.calendarweek();

 

//==< Helper-Functions >==

    Date.prototype.addDays = function (days) {

        this.setDate(this.getDate() + parseInt(days));

        return this;

    };

 

    Date.prototype.calendarweek = function () {

        //------< Date.prototype.calendarweek() >--------

        //*extends a date variable by a calendarweek() property

 

        var currentdate = this;     //current extend date-variable

        var january_01 = new Date(currentdate.getFullYear(), 0, 1);

 

        //*calendar days already passed

        var calendardays = Math.floor((currentdate - january_01) / (24 * 60 * 60 * 1000));

 

        //*week by 7days

        var calendarweek = Math.ceil((currentdate.getDay() + 1 + calendardays) / 7);

 

        //return current calendarweek

        return calendarweek;

 

        //------</ Date.prototype.calendarweek() >--------

    };

 

//==</ Helper-Functions >==

 

 

Dann kann man anschliessend eine variable von einem bestimmten Typ wie hier die Datums-variable mit der erweiterten Funktion ansprechen

                    //------< Week >------

                    //*dt.toLocaleDateString() ist immer 8/19/2020

                    col.width = 80;

                    col.cssClass = "colWeek";

                    col.alignment = "center";

                   

                    //*get current calendarweek in the year

                    var calendarweek = dt.calendarweek();

                    col.caption =  calendarweek

                    //if (culture.startsWith("de")) {

                    //    col.caption = "KW " + calendarweek + " " +  dt.toLocaleDateString("de", {  month: "2-digit", day: "2-digit" }) + "";

                    //}

                    //else {

                    //    col.caption = "CW " + calendarweek + " " + dt.toLocaleDateString("en") + "";

                    //}

                    //< Col.PastOrFuture >

                    var today = new Date();

                    var lastweekday = new Date().addDays(-30);

                    if (dt < lastweekday) {

                        col.cssClass = "colWeekLongPast";

                    }

                    else if (dt < today) {

                        col.cssClass = "colWeekPast";

                    }

                    else {

                        col.cssClass = "colWeekFuture";

                    }

                    //</ Col.PastOrFuture >

 

                    //------</ Week >------