當前位置:軟件學堂 > 資訊首頁 > 網絡編程 > 編程其他 > JS代碼制作生日提醒器

JS代碼制作生日提醒器

2012/11/6 13:04:15作者:佚名來源:網絡

移動端

【實例名稱】

JS代碼制作生日提醒器

【實例描述】

同學錄的相關網頁一般會顯示最近過生日的同學姓名,這顯得比較人性化。本例學習如何制作這種生日提醒器。

【實例代碼】

 

<html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>標題頁-學無憂(www.yzddtk.cn)</title> <SCRIPT LANGUAGE="JavaScript">   function birthday(year,month,date,person)  //一個生日對象,包括年、月日和人名   {     this.year=year     this.month=month     this.date=date     this.person=person   }   function personList()   //創建一個空的列表對象   {  }   pList=new personList()    //創建用戶列表項   pList[0]= new birthday(82,1,16,"張三")  //在列表項中添加生日對象   pList[1]= new birthday(33,9,27,"李四")   pList[2]= new birthday(66,3,1,"王五")   pList[3]= new birthday(79,4,27,"趙六")   pList[4]= new birthday(80,6,2,"小明")   pList[5]= new birthday(50,11,24,"曉紅")   var now=new Date()     //獲取今天的日期   today=new Date(now.getYear(),now.getMonth(),now.getDate()) //設置一個對象,一般為年月日0:00:00   function daysFromToday(sdate) {     return Math.round((sdate.getTime()-today.getTime())/(24*60*60*1000)) //返回一個指定日期距今天的時間   }   function writeNextBirthday(list)   {     var daysToClosest=366     var closest     for (var i in list)   //遍歷列表中的每一項     {       thisDate=new Date(today.getYear(),list[i].month,list[i].date) //獲取朋友生日的月日       if (daysFromToday(thisDate)<0)  //如果生日已經過了,則計算到下一年         thisDate.setYear(today.getYear()+1)       if (daysFromToday(thisDate)<daysToClosest) {         daysToClosest=daysFromToday(thisDate)  //計算距生日的天數-找出最近的一個         closest=i       }     }     if (daysToClosest==0)       document.write("<B>今天 "+list[closest].person+" 已經 "+ (today.getYear()-list[closest].year)+" 歲了 !!!</B><P>")     else if (daysToClosest==1)       document.write("明天 "+list[closest].person+" 即將 "+ (today.getYear()-list[closest].year)+" 歲了!<P>")     else       document.write("最近一個過生日的是"+ list[closest].person+" 還有 "+daysToClosest+" 天.<P>")

  } </SCRIPT> </head> <body> <SCRIPT LANGUAGE="JavaScript">   writeNextBirthday(pList)   //動態輸出最近要過生日的對象 </SCRIPT> </body> </html>

 

 

【運行效果】

 生日提醒器運行效果

【難點剖析】

本例使用一個對象數組包含了所有朋友列表,然后根據當前日期與列表中的月和日進行比較,使用“daysToCIosest”變量獲取最近的一個朋友生日的日期。本例中注意日期的比較和設置。

【源碼下載】

為了JS代碼的準確性,請點擊:生日提醒器 進行本實例源碼下載 

標簽: JS代碼    

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