當前位置:軟件學堂 > 資訊首頁 > 網絡編程 > 編程其他 > 全面的日期選擇功能JS代碼怎么寫

全面的日期選擇功能JS代碼怎么寫

2012/11/6 13:06:25作者:佚名來源:網絡

移動端

【實例名稱】

全面的日期選擇功能JS代碼怎么寫

【實例描述】

本例提供一些常用的日期換算.如本周、本月、上周等??稍诖a中直接調用本例的方法實現日期的換算。

【實例代碼】

 

<html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>標題頁-學無憂(www.yzddtk.cn)</title> </head> <body> 當前:<br /> <input type="text" id="date1" value=""> <input type="text" id="date2" value=""> <BR> <select onchange="eval(this. options[this.selectedIndex].touch)"> <option touch="today()">本日</option> <option touch="yesterday()">昨日</option> <option touch="toWeek()">本周</option> <option touch="lastWeek()">上周</option> <option touch="toMonth()">本月</option> <option touch="toYear()">本年</option> </select> <SCRIPT LANGUAGE="JavaScript">  function today(){   var now=new Date();       //獲取當前日期   var tmpStr = now.getYear()+ "-"; //當前年   tmpStr += now.getMonth()+1 +"-";//當前月   tmpStr += now.getDate();       //當前日   date1.value=tmpStr;             date2.value=tmpStr;            //顯示當前的月日年連接字符串  }

 function yesterday(){             //獲取昨天的日期   var now=new Date();   var tmpStr = now.getYear()+ "-"; //當前年   tmpStr += now.getMonth()+1 +"-"; //當前月   tmpStr += (now.getDate() - 1);    //當前日   date1.value=tmpStr;   date2.value=tmpStr;            //顯示昨天的月日年連接字符串  }

 function toYear(){   var now=new Date();   var tmpStr;   tmpStr = now.getYear()+ "-";  //當前年   date1.value=tmpStr+"1-1";     //年的開始   date2.value=tmpStr+"12-31";   //年的結束  }

 function toMonth(){   var now=new Date();   var tmpStr,dt;   tmpStr = now.getYear()+ "-";  //當前年   tmpStr += now.getMonth()+1 +"-"; //月份+1      tmpStr += (now.getDate() - now.getDate())+1; //天數+1   date1.value=tmpStr;   dt = new Date(now.getFullYear(), now.getMonth() + 1, 0);   date2.value=replStr(dt.toLocaleDateString());  }

 function lastWeek(){                //上周   var now=new Date();   var currentWeek = now.getDay(); //獲取一周中的第幾天   if ( currentWeek == 0 )        //0在英文中表示第一天,中文表示最后一天     {     currentWeek = 7;     }

  var monday = now.getTime() - (currentWeek+6)*24*60*60*1000;   monday = replStr(new Date(monday).toLocaleDateString()); //獲取周一   var sunday = now.getYear()+ "-";   sunday += now.getMonth()+1 +"-";                        //獲取周日   if ( currentWeek == 7 )   {    sunday += (now.getDate() - 7);   }else{    sunday += (now.getDate() - currentWeek);   }     date1.value=monday;   date2.value=sunday;                                    //顯示周一到周日的時間  }

 function toWeek(){   var now=new Date();   var currentWeek = now.getDay();   if ( currentWeek == 0 )     {     currentWeek = 7;     }   var monday = now.getTime() - (currentWeek-1)*24*60*60*1000; //獲取周一   var sunday = now.getTime() + (7-currentWeek)*24*60*60*1000;   monday = replStr(new Date(monday).toLocaleDateString()); //獲取周日   sunday = replStr(new Date(sunday).toLocaleDateString());   date1.value=monday;   date2.value=sunday;//顯示周一到周日的時間  }

 function replStr(str)  {   str = str.replace("年","-");//替換字符串-顯示中文日期   str = str.replace("月","-");   str = str.replace("日","");   return str;  } </SCRIPT> </body> </html>

 

【運行效果】

 全面的日期選擇功能運行效果

【難點剖析】

本例中的方法都是對日期對象進行換算,實現所要求的數據。其中“toYear”表示獲取當前年的開始日期和結束日期,“toMonth”獲取當前月的開始日期和結束日期,“lastWeek”獲取上周的開始日期和結束日期。

【源碼下載】

為了JS代碼的準確性,請點擊:全面的日期選擇功能 進行本實例源碼下載 

欧美一级一极性活片免费观看,欧美一级艳片欧美精品,欧美一级性爱大片,欧美一级囗交视频 视频,欧美一级特黄录像视频