[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 393
  • Last Modified:

mydate.getWeek() question

This is not working, it doesn't even bring up the default banner.  I did have it working before when I sorted the new images based on month / day see: http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21192354.html 
but now I want to change it so it switches to images based on weeks. (No need for otherwise)

<SCRIPT>
var mydate=new Date()
var week=mydate.getWeek()+1
if (week<10)
week="0"+week
var newimg="http://home-url.com/Images/Calendar_Banners/banner"+week+".gif"
document.write('<center><img src="'+newimg+'" width="780" height="175" onError="this.src=\'http://home-url.com/Images/Banners/Default_banner.gif\';">')
</SCRIPT>
0
ALEx604
Asked:
ALEx604
  • 4
  • 3
1 Solution
 
GwynforWebCommented:
the date object does not have a getWeek() method.
0
 
archrajanCommented:
use this script to get the week
function getWeek(year,month,day){
    //lets calc weeknumber the cruel and hard way :D
    //Find JulianDay
    month += 1; //use 1-12
    var a = Math.floor((14-(month))/12);
    var y = year+4800-a;
    var m = (month)+(12*a)-3;
    var jd = day + Math.floor(((153*m)+2)/5) +
                 (365*y) + Math.floor(y/4) - Math.floor(y/100) +
                 Math.floor(y/400) - 32045;      // (gregorian calendar)
    //var jd = (day+1)+Math.Round(((153*m)+2)/5)+(365+y) +
    //                 Math.round(y/4)-32083;    // (julian calendar)
   
    //now calc weeknumber according to JD
    var d4 = (jd+31741-(jd%7))%146097%36524%1461;
    var L = Math.floor(d4/1460);
    var d1 = ((d4-L)%365)+L;
    NumberOfWeek = Math.floor(d1/7) + 1;
    return NumberOfWeek;        
}
0
 
archrajanCommented:
or you use this one
script type="text/javascript" language="JavaScript"><!--
function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function getWeek(year,month,day) {
    var when = new Date(year,month,day);
    var newYear = new Date(year,0,1);
    var offset = 7 + 1 - newYear.getDay();
    if (offset == 8) offset = 1;
    var daynum = ((Date.UTC(y2k(year),when.getMonth(),when.getDate(),0,0,0) - Date.UTC(y2k(year),0,1,0,0,0)) /1000/60/60/24) + 1;
    var weeknum = Math.floor((daynum-offset+7)/7);
    if (weeknum == 0) {
        year--;
        var prevNewYear = new Date(year,0,1);
        var prevOffset = 7 + 1 - prevNewYear.getDay();
        if (prevOffset == 2 || prevOffset == 8) weeknum = 53; else weeknum = 52;
    }
    return weeknum;
}

var now = new Date();
document.write('Week number = ' + getWeek(y2k(now.getYear()),now.getMonth(),now.getDate()));
//--></script>
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ALEx604Author Commented:
I will try this out and if it works I will accept it later sothat there ismore points to give.
0
 
ALEx604Author Commented:
Ok not working...
I'm not even getting the onError banner now.
Here is the entire html code for the page (unrelated stuff snipped with comment):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML><HEAD>
<!-- bunch of meta tag stuff here -->
<script type="text/javascript">
function getWeek(year,month,day){
    month += 1; //use 1-12
    var a = Math.floor((14-(month))/12);
    var y = year+4800-a;
    var m = (month)+(12*a)-3;
    var jd = day + Math.floor(((153*m)+2)/5) +
                 (365*y) + Math.floor(y/4) - Math.floor(y/100) +
                 Math.floor(y/400) - 32045;      // (gregorian calendar)
    var d4 = (jd+31741-(jd%7))%146097%36524%1461;
    var L = Math.floor(d4/1460);
    var d1 = ((d4-L)%365)+L;
    NumberOfWeek = Math.floor(d1/7) + 1;
    return NumberOfWeek;        
}
</SCRIPT>
</HEAD>
<BODY>
<!-- Bunch of image map junk -->
<!-- Bunch of ISM Update code junk here (not java all serverside) -->
<!-- MENU CODE / BANNER STARTS -->
<SCRIPT>
var week=mydate.getWeek()
if (week<10)
week="0"+week
var newimg="http://my-url.com/Images/Calendar_Banners/banner"+week+".gif"
document.write('<center><img src="'+newimg+'" width="780" height="175" onError=" this.src=\'http://my-url.com/Images/Banners/Default_banner.gif\';">')
</SCRIPT>
<!-- MENU CODE STUFF HERE -->
<!-- THE REST OF THE BODY TEXT STUFF FOR THE WEBPAGE HERE --></body</html>
0
 
ALEx604Author Commented:
Trying it the other way.. bbiab (Nov 23. 16:00)
0
 
ALEx604Author Commented:
The y2k version worked, (although i don't think it was because of y2k :P )

I will wait till I have 125 in points before I award this to archrajan

Thanks a heap!
0
 
archrajanCommented:
ok... thank you
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now