編程(Programming)是編定程序的中文簡稱,就是讓計算機代碼解決某個問題,對某個計算體系規定一定的運算方式,使計算體系按照該計算方式運行,并最終得到相應結果的過程。為了使計算機能夠理解(understand)人的意圖,人類就必須將需解決的問題的思路、方法和手段通過計算機能夠理解的形式告訴計算機,使得計算機能夠根據人的指令一步一步去工作,完成某種特定的任務。這種人和計算體系之間交流的過程就是編程。 【實例名稱】 全面的日期選擇功能JS代碼怎么寫 【實例描述】 本例提供一些常用的日期換算.如本周、本月、上周等?稍诖a中直接調用本例的方法實現日期的換算。 【實例代碼】 <html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>標題頁-本站(www.xue51.com)</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代碼的準確性,請點擊:全面的日期選擇功能 進行本實例源碼下載
使用編程語言寫的程序,由于每條指令都對應計算機一個特定的基本動作,所以程序占用內存少、執行效率高。 |