Just nu i M3-nätverket
Gå till innehåll
mac3

hjälp med ett veckor script

Rekommendera Poster

mac3

Hej jag försöker att göra ett veckor script 

jag går bet eftersom mina knappar nästa och före

visar ibland rätt och jätte fel. 

 

jag vill att scriptet ska visa

 

vecka 11 till  x som är 14 eftersom jag har hårdkoda den här lösningen 

 

jag tar datumet och fixa till att det blir måndag sedan kolla jag viken vecka just den måndag som har

sedan plussar jag på 7 dagar och ta bort 7 dagar 


<!DOCTYPE html>
<html>
    <head>
        <title>Untitled Document</title>
        <meta charset="UTF-8">
        <meta name="description" content="">
        <meta name="keywords" content="">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
        <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
        <script>

            today = new Date();
            var nextdate = today;
            var prevdate = today;
            var range = 4;
            var day = 7;
            var week = false;
            var weeklist = false;
            
            Object.prototype.removeItem = function (key) {
                if (!this.hasOwnProperty(key))
                    return
                    if (isNaN(parseInt(key)) || !(this instanceof Array))
                    delete this[key]
                    else
                        this.splice(key, 1)
            };
            
            Date.prototype.addDays = function (d) {
                if (d) {
                    var t = this.getTime();
                    t = t + (d * 86400000);
                    this.setTime(t);
                }
            };

            Date.prototype.getWeek = function(){
                // We have to compare against the first monday of the year not the 01/01
                // 60*60*24*1000 = 86400000
                // 'onejan_next_monday_time' reffers to the miliseconds of the next monday after 01/01

                var day_miliseconds = 86400000,
                    onejan = new Date(this.getFullYear(),0,1,0,0,0),
                    onejan_day = (onejan.getDay()==0) ? 7 : onejan.getDay(),
                    days_for_next_monday = (8-onejan_day),
                    onejan_next_monday_time = onejan.getTime() + (days_for_next_monday * day_miliseconds),
                    // If one jan is not a monday, get the first monday of the year
                    first_monday_year_time = (onejan_day>1) ? onejan_next_monday_time : onejan.getTime(),
                    this_date = new Date(this.getFullYear(), this.getMonth(),this.getDate(),0,0,0),// This at 00:00:00
                    this_time = this_date.getTime(),
                    days_from_first_monday = Math.round(((this_time - first_monday_year_time) / day_miliseconds));

                var first_monday_year = new Date(first_monday_year_time);

                // We add 1 to "days_from_first_monday" because if "days_from_first_monday" is *7,
                // then 7/7 = 1, and as we are 7 days from first monday,
                // we should be in week number 2 instead of week number 1 (7/7=1)
                // We consider week number as 52 when "days_from_first_monday" is lower than 0,
                // that means the actual week started before the first monday so that means we are on the firsts
                // days of the year (ex: we are on Friday 01/01, then "days_from_first_monday"=-3,
                // so friday 01/01 is part of week number 52 from past year)
                // "days_from_first_monday<=364" because (364+1)/7 == 52, if we are on day 365, then (365+1)/7 >= 52 (Math.ceil(366/7)=53) and thats wrong

                return (days_from_first_monday>=0 && days_from_first_monday<364) ? Math.ceil((days_from_first_monday+1)/7) : 52;
            }

            function weeksInYear(year) {
                var d = new Date(year, 11, 31);
                var week = getWeekNumber(d)[1];
                return week == 1? getWeekNumber(d.setDate(24))[1] : week;
            }

            function getWeekNumber(d) {
                // Copy date so don't modify original
                d = new Date(+d);
                d.setHours(0,0,0);
                // Set to nearest Thursday: current date + 4 - current day number
                // Make Sunday's day number 7
                d.setDate(d.getDate() + 4 - (d.getDay()||7));
                // Get first day of year
                var yearStart = new Date(d.getFullYear(),0,1);
                // Calculate full weeks to nearest Thursday
                var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7)
                // Return array of year and week number
                return [d.getFullYear(), weekNo];
            }

            function firstDayOfWeek (year, week) {

                // Jan 1 of 'year'
                var d = new Date(year, 0, 1),
                    offset = d.getTimezoneOffset();

                // ISO: week 1 is the one with the year's first Thursday 
                // so nearest Thursday: current date + 4 - current day number
                // Sunday is converted from 0 to 7
                d.setDate(d.getDate() + 4 - (d.getDay() || 7));

                // 7 days * (week - overlapping first week)
                d.setTime(d.getTime() + 7 * 24 * 60 * 60 * 1000 
                          * (week + (year == d.getFullYear() ? -1 : 0 )));

                // daylight savings fix
                d.setTime(d.getTime() 
                          + (d.getTimezoneOffset() - offset) * 60 * 1000);

                // back to Monday (from Thursday)
                d.setDate(d.getDate() - 3);
                (d.getDay)

                return d;

            }

            function lastDayOfWeek (year, week) {

                // Jan 1 of 'year'
                var d = new Date(year, 0, 1),
                    offset = d.getTimezoneOffset();

                // ISO: week 1 is the one with the year's first Thursday 
                // so nearest Thursday: current date + 4 - current day number
                // Sunday is converted from 0 to 7
                d.setDate(d.getDate() + 4 - (d.getDay() || 7));

                // 7 days * (week - overlapping first week)
                d.setTime(d.getTime() + 7 * 24 * 60 * 60 * 1000 
                          * (week + (year == d.getFullYear() ? -1 : 0 )));

                // daylight savings fix
                d.setTime(d.getTime() 
                          + (d.getTimezoneOffset() - offset) * 60 * 1000);

                // back to Monday (from Thursday)
                d.setDate(d.getDate() - 3);
                d.setDate(d.getDate()+6);

                return d;

            }

            function getWeekNumber(d) {
                // Copy date so don't modify original
                d = new Date(+d);
                d.setHours(0,0,0);
                // Set to nearest Thursday: current date + 4 - current day number
                // Make Sunday's day number 7
                d.setDate(d.getDate() + 4 - (d.getDay()||7));
                // Get first day of year
                var yearStart = new Date(d.getFullYear(),0,1);
                // Calculate full weeks to nearest Thursday
                var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7)
                // Return array of year and week number
                return [d.getFullYear(), weekNo];
            }

            function run(){
                i = 1;
                d = 0;
                tmp="";
                weeks ="";

                end = nextrange;
                rweek = weeklist;
                $('#week').html('');

                $('#week').append('<table>');
                var s = 0;

                for (;rweek[s] {
                    weeks+= '<td colspan="7">'+rweek[s][0]+'</td>';  
                    s++;  
                }
                $('#week').append('<tr>'+weeks+'</tr>');



                while(i <= (day*range)){
                    if(d == 6){
                        tmp+='<td>'+d+'</td>';
                        d=0;
                    }else{  
                        tmp+='<td>'+d+'</td>';
                        d++;
                    }

                    i++;  
                }
                $('#week').append('<tr>'+tmp+'</tr>');   
                $('#week').append('</table>');


            }

            function getweeknummer(d) {

                return d.getWeek();
            }

            function getMonday(d) {
                d = new Date(d);
                var day = d.getDay(),
                    diff = d.getDate() - day + (day == 0 ? -6:1); // adjust when day is sunday
                return new Date(d.setDate(diff));
            }


            function getweeklist(){

               var tmp =  [[],[]];
                start = 0;
                end = range;
                date = today;
                mon = getMonday(date);
                prevdate = mon;
                r = getMonday(date);
           
                r = new Date();
                
       
                r = new Date(r.setDate(r.getDate()-7));
                while(start < end){
                
                        

                  

                    r = new Date(r.setDate(r.getDate()+7));
           
                    nextdate = r;
                    tmp[start] = [];
                    tmp[start][0] = getWeekNumber(r)[1];
                    tmp[start][1] = r;
                    
                    start++;  
                }

                   
                return tmp
            }




            function getweeklistnext(){

                tmp =  weeklist;
                tmp.removeItem(0);
            
               
                start = tmp.length;
                end = (tmp.length+1);
                
                r = tmp[tmp.length-1][1];
         
                mon = getMonday(date);
                prevdate = mon;
               
                while(start < end){
                  
                    
                    tmp[start] = [];
                    tmp[start][0]= getWeekNumber(r)[1];
                    tmp[start][1] =r;
                    r = new Date(r.setDate(r.getDate()+7));
                    start++;  
                }


                return tmp
            }

            function getPrevMonday(a) {
                return new Date(a.getTime() - ( (6+(a.getDay()||7))*24*60*60*1000 ));
            };
            function getweeklistprev(){
                tmpe = [];
                tmp1 = []; 
                tmp =  weeklist;
                tmp.removeItem(tmp.length-1);
               
                console.log(tmp);
                start = 0;
                end = 1;
               
                date = tmp[0][1];
                
                 r =date;
          
                while(start < end){
                    r = new Date(r.setDate(r.getDate()-7));
                    
                    tmpe[0] = [];
                    tmpe[0][1] = r;
                    tmpe[0][0]= getWeekNumber(r)[1];
                  
                    start++;  
                }

                var children = tmpe.concat(tmp);
        
                
                return children;
            }

            $(function(){

                if(week==false){
                    week = today.getWeek();

                }

                nextrange = false;

                if(nextrange==false){

                    nextrange = (today.getWeek()+range-1);
                }

                $('#console').append('Datum '+today.toDateString()+'<br>');
                $('#console').append('Vecka '+today.getWeek()+'<br>');


                $('#console').append('Vecka '+range+' framåt '+(today.getWeek()+range-1)+'<br>');
                $('#console').append('Vecka '+range+' bakåt '+(today.getWeek()-range)+'<br>');

                $('#console').append('Datum p├Ñ '+(today.getWeek()+range-1)+' vecka p├Ñ f├Ârsta dag i veckan '+(today.getWeek()-range)+' m├Ñndag '+firstDayOfWeek(today.getFullYear(),(today.getWeek()-range)).toDateString()+' <br>');
                $('#console').append('Datum på  sista dagen i '+(today.getWeek()-range)+' veckan '+lastDayOfWeek(today.getFullYear(),today.getWeek()-range).toDateString()+'<br>');

                $('#console').append('År innan '+(today.getFullYear()-1)+'<br>');
                $('#console').append('År '+today.getFullYear()+'<br>');
                $('#console').append('├àr f├Âre '+(today.getFullYear()+1)+'<br>');

                $('#console').append('Antar veckor på År innan '+weeksInYear(today.getFullYear()-1)+'<br>');
                $('#console').append('Antar veckor på År '+weeksInYear(today.getFullYear())+'<br>');
                $('#console').append('Antar veckor p├Ñ ├àr f├Âre '+weeksInYear(today.getFullYear()+1)+'<br>');
                // $('#console').append('list bak '+getweeklistnext()+'<br>');
                // $('#console').append('list fram '+getweeklistprev()+'<br>');


                weeklist = getweeklist();

                run();

                $('#console').append('<input type="button" id="prev" value="Bakåt">');
                $('#console').append('<input type="button" id="next" value="Framåt">');
                $('#prev').click(function(){
                    console.log('k├Âr b├Ñk├Ñt');

                    weeklist = getweeklistprev();

                    console.log(weeklist);
                    
                    run();

                });

                $('#next').click(function(){
                    console.log('k├Âr next');
                    weeklist = getweeklistnext();

                    console.log(today.toDateString());
                    run();

                });

            });

        </script>
    </head>
    <body>
        <div id="console"></div>
        <div id="week"></div>

    </body>
</html> 

Dela detta inlägg


Länk till inlägg
Dela på andra webbplatser
ChromaWoods

Att bolla runt med Date() kan vara risky business. Ditt script ser rätt komplext ut, har du funderat på att använda ett lib? Stand on the shoulders of giants..

 

Jag kan varmt rekommendera:

http://momentjs.com/

Dela detta inlägg


Länk till inlägg
Dela på andra webbplatser

Skapa ett konto eller logga in för att kommentera

Du måste vara medlem för att kunna kommentera

Skapa ett konto

Skapa ett nytt konto på vårt forum. Det är lätt!

Registrera ett nytt konto

Logga in

Redan medlem? Logga in här.

Logga in nu



×
×
  • Skapa nytt...