JS implementation of the month-day date display code example:
A lot of web pages at the top of the page or other locations have a display date, and can also automatically beat, it is better, the following is a code example of how to achieve this effect.
The code example is as follows:
<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><Metaname= "Author"content= "http://www.softwhy.com/" /><title>Ant Tribe</title><Scripttype= "Text/javascript">varWeek;varweeks;vardate;varTime ;varhour;varminute;varsecond;functionGettimenow () { time=NewDate (); Hour=time.gethours (); Minute=time.getminutes (); Second=time.getseconds (); Weeks=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]; Week=Weeks[time.getday ()]; Date=(Time.getfullyear ())+"years"+(Time.getmonth ()+1)+"Month"+time.getdate ()+"Day"+" "; Nowtime.innerhtml="Current Time:"+Date+Week+" "+Hour+":"+minute+":"+second;} Window.onload=function(){ varNowtime=document.getElementById ("Nowtime"); SetInterval ("Gettimenow ()", +); }</Script></Head><Body> <DivID= "Nowtime"></</div> </Body></HTML>
The above code realizes our function, can display the current system's date and time in real-time, the following briefly introduces its realization process.
I. Principle of implementation:
the principle is very simple, first create a function Gettimenow (), when running this function, you can write the current month and date time to the Div, the way to get the month and day is the Date object function. Then use the SetInterval () function to execute the Gettimenow () function once every second, so that the current datetime in the div can be displayed in fact.
Two. Code comments:
1.var Week, declaring variables, term storage week, the following several variable declaration principle is also the case.
2.function Gettimenow () {}, this function can get the date time of the current system and write to the Div.
3.time=new Date ();, creates the current time object.
4.hour=time.gethours (), gets the hour of the current time.
5.minute=time.getminutes (), gets the minute of the current time.
6.second=time.getseconds (), gets the seconds of the current time.
7.weeks=["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"], create a number, the element in the array is the week.
The 8.week=weeks[time.getday ()],getday () function can translate a value of 0-6, taking advantage of this feature, which can be obtained at the current day of the week.
9.date= (Time.getfullyear ()) + "year" + (Time.getmonth () +1) + "month" +time.getdate () + "Day" + "", string connection, connecting the current month day.
10.nowtime.innerhtml = "Current time:" +date+week+ "" +hour+ ":" +minute+ ":" +second, writes the month-date time and dates to the Div.
Three. Related reading:
1. Refer to the Date object section of ECMAScript for a Date object and related functions.
2. The SetInterval () function can be found in the setinterval () Function usage section.
The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=10388
For more information, refer to: http://www.softwhy.com/javascript/
JS implementation of the month-day date display code example