Menu
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 [B]Week Date
I would love if someone could point me in the right direction as to how I can accomplish my 3rd step.
Thank you!
[CODE]<form action=""><div>
<select name="Year">
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
</select>
<select name="Week Num">
<option value="WK01">WK01</option>
<option value="WK02">WK02</option>
<option value="WK03">WK03</option>
<option value="WK04">WK04</option>
<option value="WK05">WK05</option>
</select>
<input type="text" name="Week Name"></div>
<div><input type="submit" value="submit"></div>
</form>
[/CODE]
<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,[COLOR="Red"]0[/COLOR]);
var d2=new Date(inp1+1,0,[COLOR="Red"]0[/COLOR]);
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()">
<i>
</i><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,[COLOR="SeaGreen"][B][SIZE="3"][SIZE="5"]0[/SIZE][/SIZE][/B][/COLOR]); <br/>
var d2=new Date(inp1+1,0,[COLOR="SeaGreen"][B][SIZE="5"]1[/SIZE][/B][/COLOR]);
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()">
[code=html][/code]
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...[code=html]<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>
[/code]
[code=html]<select id="yearList">
<script language="javascript">yearDropDown();</script>
</select>
<select id="weekList">
<script language="javascript">yearDropWeek(document.getElementById("yearList").value);</script>
</select>
<input type="button" onClick="andTheDateIs(document.getElementById('weekList').value,document.getElementById('yearList').value)" value="get result!" />
<h3 id="resultBox"></h3>[/code]
[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>");
}
}[/CODE]
[CODE]document.write("<option value='"+b+"'>"+y+"</option>");[/CODE]
[CODE]document.write("<option value='"+b+"'>WK"+y+"</option>");[/CODE]
[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(){
for(y=1;y<=52;y++){
var b=y;
if(y<10){
y="0"+y;
}
document.write("<option value='"+b+"'>WK"+y+"</option>");
}
}
function andTheDateIs(wkNum,yrNum){
var theDate=Date.UTC(yrNum,0,1);
var result=theDate+(wkNum*604800000);
var resultDate=new Date(result);
var dateNum = resultDate.getDate();
var dateDay = resultDate.getDay();
var dateMonth = resultDate.getMonth();
if(dateDay!==6){
result=result-((dateDay+1)*86400000);
resultDate=new Date(result);
dateNum = resultDate.getDate();
dateDay = resultDate.getDay();
dateMonth = resultDate.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>
[/CODE]
[CODE]
<select id="yearList">
<script language="javascript">yearDropDown();</script>
</select>
<select id="weekList">
<script language="javascript">yearDropWeek();</script>
</select>
<input type="button" onClick="andTheDateIs(document.getElementById('weekList').value,document.getElementById('yearList').value)" value="get result!" />
<h3 id="resultBox"></h3>
[/CODE]
0.1.9 — BETA 6.16