Date: Week of the year : Date « Development « JavaScript DHTML






Date: Week of the year

<html>
<head>

</head>

<body>
<SCRIPT Language="JavaScript">
    function weekNo() {
        
        var totalDays = 0;
        
        now = new Date();
        years=now.getYear();
    
        var days = new Array(12); 
        days[0] = 31;
        days[2] = 31;
        days[3] = 30;
        days[4] = 31;
        days[5] = 30;
        days[6] = 31;
        days[7] = 31;
        days[8] = 30;
        days[9] = 31;
        days[10] = 30;
        days[11] = 31;
 
        // is it a leap year
        //////////////////////////////////////////////////
        // has some year 2000 problem 
        // should check the year by dividing 4000
        //
        //////////////////////////////////////////////////
        if (Math.round(now.getYear()/4) == now.getYear()/4) {
            days[1] = 29
        }else{
            days[1] = 28
        }
 
        // If this is January 
        if (now.getMonth() == 0) { 
            totalDays = totalDays + now.getDate();
        }else{
            var curMonth = now.getMonth();
            for (var count = 1; count <= curMonth; count++) {
                totalDays = totalDays + days[count - 1];
        }
        totalDays = totalDays + now.getDate();
    }

    var agt=navigator.userAgent.toLowerCase();
    
    if ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1)) {
        var firstday=new Date("01/01/"+String(now.getYear())).getDay();
    }else {
        var firstday=new Date("01/01/"+String(1900+now.getYear())).getDay();
    }
    
    var diff=7-firstday+1;
    var week = Math.round((totalDays+diff-firstday)/7); return week;
}

document.write("Week "+weekNo()+" of "+years)

</SCRIPT>
</body>

</html>




           
       








Related examples in the same category

1.Demo all methods in Date class
2.how many days Between two dates
3.Today's Date
4.Display date: day month year in string
5.Date: date, month, and year.
6.Set date: setDate, setHour
7.UTC time: getUTCDate returns the Universal Coordinated Time
8.Display weekday: name of the current day
9.Display full date : complete date with the day name and month name
10.Display time: continues writing time per second.
11.Display current date: year, month day in number
12.Extending the Date Object to Include Some New Methods
13.Methods and Properties of the Date Object
14.Using the Date Object
15.Output day
16.A Dynamic Welcome Message
17.How Many Days Until Christmas
18.Summer Games Countdown
19.Simple Date Validation
20.GMT Calculator
21.Days Before Next Christmas Xmas
22.Get how many days before a date