JavaScript dynamically gets the current date, time, and week code

Source: Internet
Author: User
Tags current time getdate local time time interval

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>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.