I am fairly new to javascript...so please bear with me...
I need to build a basic pop-up that will allow users to do the following:
1) Enter Year.....this can be hardcoded (no problem)
2) Enter Week Number.... this may also be hardcoded (e.g. WK01, WK02)
3) Once the above fields are complete, then when the user submits...the textbox should provide the correct Week Date that corresponds to the Year and Week Number.
I would love if someone could point me in the right direction as to how I can accomplish my 3rd step.
I am not sure if the "Week Num" needs to be generated by a function like getWeek()....but ultimately I am trying to create a simple lookup form where a user can enter a year, and out of the possible 52 weeks,..they can view the appropriate week date associated with the selected week number.
For example...if the user selected 2008..then WK04..the result for week date should be Jan 26 2008.
<body>
<script type="text/javascript">
Date.prototype.week=function(d2){
var i=1, A=[];
while(this<d2) {
var a = new Date(this.setDate(this.getDate()+i) ) ;
if(a.getDay()==6) A[A.length]=a ;
}
return A;
}
function f(){
var inp1 =document.getElementById('year');
inp1=Number(inp1.options[inp1.selectedIndex].value);
var inp2 =document.getElementById('week');
inp2=Number(inp2.options[inp2.selectedIndex].value);
//alert(inp1+"\n"+inp2)
var d=new Date(inp1,0,0);
var d2=new Date(inp1+1,0,0);
alert(d+"\n"+d2);
var result=d.week(d2);
//alert(result);
//alert(result.length);
alert(result[inp2-1]);
}
</script>
<select id="year">
<option value="2008">2008</option>
<option value="2007">2007</option>
<option value="2006">2006</option>
</select>
<select id="week">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<input type="button" value="click me" onclick="f()">
Last edited by Ayşe; 06-25-2008 at 11:59 AM.
The Time Through Ages
In the Name of Allah, Most Gracious, Most Merciful
1. By the Time,
2. Verily Man is in loss,
3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.
<body>
<script type="text/javascript">
Date.prototype.week=function(d2){
var m=["January","February","March","April","May","June","July","August","September","October","November","December"];
var i=1, A=[];
while(this<d2) {
var a = new Date(this.setDate(this.getDate()+i) ) ;
var day=a.getDate();
var gd=day<10?"0"+day:day;
var str = gd +"-" +m[a.getMonth()].substr(0,3)+ "-"+String(a.getFullYear()).substr(2);
if(a.getDay()==6) A[A.length]=str ;
}
return A;
}
function f(){
var inp1 =document.getElementById('year');
inp1=Number(inp1.options[inp1.selectedIndex].value);
var inp2 =document.getElementById('week');
inp2=Number(inp2.options[inp2.selectedIndex].value);
//alert(inp1+"\n"+inp2)
var d=new Date(inp1,0,0);
var d2=new Date(inp1+1,0,1);
alert(d+"\n"+d2);
var result=d.week(d2);
//alert(result);
//alert(result.length);
alert(result[inp2-1]);
}
</script>
<select id="year">
<option value="2008">2008</option>
<option value="2007">2007</option>
<option value="2006">2006</option>
</select>
<select id="week">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<input type="button" value="click me" onclick="f()">
Last edited by Ayşe; 06-25-2008 at 12:33 PM.
The Time Through Ages
In the Name of Allah, Most Gracious, Most Merciful
1. By the Time,
2. Verily Man is in loss,
3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.
lol seeems like im too late... was playing football then at a friends but as promised when i got back i came up with the script and it works fine and you would never need to hadcode the year because this grabs the last 2 years and next 5 years to choose from...
anyway if you wanna test it, this goes in the head section:
HTML Code:
<script language="javascript">
function yearDropDown(){
var theDate=new Date();
var theYear=theDate.getFullYear()-2;
for(y=0;y<6;y++){
document.write("<option value='"+theYear+"'>"+theYear+"</option>");
theYear++;
}
}
function yearDropWeek(selectedYear){
for(y=1,b=1;y<=52;y++,b++){
if(y<10){
y="0"+y;
}
document.write("<option value='"+b+"'>"+y+"</option>");
}
}
function andTheDateIs(wkNum,yrNum){
var theDate=Date.UTC(yrNum,0,1);
var diff=wkNum*604800000;
var result=theDate+diff;
var resultDate=new Date(result);
var dateNum = resultDate.getDate();
var dateDay = resultDate.getDay();
var dateMonth = resultDate.getMonth();
if(dateDay!==6){
var minusMilli = (dateDay+1)*86400000;
result=result-minusMilli;
var notSatDate=new Date(result);
dateNum = notSatDate.getDate();
dateDay = notSatDate.getDay();
dateMonth = notSatDate.getMonth();
}
var month=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
document.getElementById("resultBox").innerHTML="Sat: "+dateNum+"-"+month[dateMonth].substr(0,3)+"-"+yrNum;
}
</script><style type="text/css">
input, select{
width:80px;
}
</style>
I am in the process of applying it to my pop-up....one question though...I was reviewing the following function:
Code:
function yearDropWeek(selectedYear){
for(y=1,b=1;y<=52;y++,b++){
if(y<10){
y="0"+y;
}
document.write("<option value='"+b+"'>"+y+"</option>");
}
}
Is it possible that a year may have more than 52 weeks?
...and my final question is if I needed to modify the this function for the Week Number dropdown box so that it looks like WK01 rather than just 1...is this easy to do?
i originally coded it to look like that but it looked heavy on the eye, as far as i am aware there is only 52 weeks a year even in a leep year googled it.
Bookmarks