Copy the snippet directly and paste it into the HTML structure to display (<span> paste here </span>)
Get format: Year, month, day
The code is as follows |
Copy Code |
var date = new Date (); var year = Date.getfullyear (); var month = Date.getmonth () +1; var day = Date.getdate (); document.write ("Today is" +year+ "year" +month+ "month" +day+ "Day"); |
Get format: Year, month, day, time, minute
The code is as follows |
Copy Code |
var date = new Date (); var year = Date.getfullyear (); var month = Date.getmonth () +1; var day = Date.getdate (); var hours = date.gethours (); var min = date.getminutes (); document.write ("Today is" +year+ "year" +month+ "month" +day+ "Day" +hours+ ":" +min); |
Get format: Year, month, day, week
The code is as follows |
Copy Code |
var date = new Date (); var year = Date.getfullyear (); var month = Date.getmonth () +1; var day = Date.getdate (); var Xingqi = Date.getday (); Switch (Xingqi) { Case 0: Xingqi = "Day"; Break Case 1: Xingqi = "one"; Break Case 2: Xingqi = "two"; Break Case 3: Xingqi = "three"; Break Case 4: Xingqi = "Four"; Break Case 5: Xingqi = "Five"; Break Case 6: Xingqi = "Six"; Break } document.write ("Today is" +year+ "year" +month+ "month" +day+ "Day" + "Week" +xingqi); |
It's written as a function
code is as follows |
copy code |
function ShowTime () { var show_day=new Array (' Monday ', ' Tuesday ', ' Wednesday ', ' Thursday ', ' Friday ', ' Saturday ', ' Sunday '); var time=new Date (); var year=time.getyear (); var month=time.getmonth (); var date=time.getdate (); var day=time.getday (); var hour=time.gethours (); var minutes=time.getminutes (); var second=time.getseconds (); month<10?month= ' 0 ' +month:month; month=month+1; hour<10?hour= ' 0 ' +hour:hour; minutes<10?minutes= ' 0 ' +minutes:minutes; second<10?second= ' 0 ' +second:second; var now_time= ' current time: ' +year+ ' year ' +month+ ' month ' +date+ ' Day ' + ' ' +show_day[day-1]+ ' ' +hour+ ': ' +minutes+ ': ' +second; document.getelementbyid (' Showtime '). Innerhtml=now_time; settimeout ("ShowTime ();", 1000); } |
The above example is all to get the client if you want to get the server side we can use Ajax to instance
Name: Server clock (one read, real-time display)
Function: Displays the server-side time on the client browser.
Principle:
1. Get the date and time of the service end.
2. The time difference between the server and the client can be obtained according to the client browser.
3. Server Clock = client's clock (change value) + Time difference (fixed value)
The code is as follows |
Copy Code |
<title></title> <body> Current: <div id= "Time" ></div> <script> var XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");//Create XmlHttp Object Xmlhttp.open ("Head", "Http://www.111cn.net", false); from which server to get the time Xmlhttp.send (); Connecting to a server var offset = Date.parse (Xmlhttp.getresponseheader ("Date"));//Get Time in header Offset-= (new Date). GetTime (); Get the interval between local time and server time function ShowTime ()//Display Time method { var d = new Date; Get current time D.settime (D.gettime () +offset); Get current server time through server and local time interval document.getElementById (' time '). Innerhtml=d.tolocalestring ()//Display server times } SetInterval ("ShowTime ()", "1000"); </script> </body> |