Javascript code for dynamically obtaining the current date, time, and week

Source: Internet
Author: User

This article summarizes some common Javascript code used to dynamically obtain the current date, time, and week.

Copy the code snippet and paste it to the html structure to be displayed (<span> paste it here </span>)

Format: year, month, and day

The Code is as follows:

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 ");

Format: year, month, day, hour, and minute

The Code is as follows:

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 );

Format: year, month, day, week

The Code is as follows:

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 = "1 ";
Break;
Case 2:
Xingqi = "2 ";
Break;
Case 3:
Xingqi = "3 ";
Break;
Case 4:
Xingqi = "4 ";
Break;
Case 5:
Xingqi = "5 ";
Break;
Case 6:
Xingqi = "6 ";
Break;
}

Document. write ("today is" + year + "year" + month + "month" + day + "day" + "Week" + xingqi );

Written as a function

The Code is as follows:

Function showTime (){
Var show_day = new Array ('monday', 'tuesday', 'wedday', 'thurs', '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 );
}

All of the above instances are obtained from the client. If you want to obtain the server, you can use ajax for the instance.

Name: Server clock (read once, real-time display)
Function: display the Time of the server on the client browser.
Principle:
1. Get the date and time of the server.
2. the time difference between the server and the client can be obtained based on the browser time of the client.
3. Server clock = client clock (change value) + time difference (fixed value)
 

The Code is as follows:
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> </title>
</Head>
<Body>
Current time: <div id = "time"> </div>
<Script>
Var XmlHttp = new ActiveXObject ("Microsoft. XmlHttp"); // create an XMLHTTP object
XmlHttp. open ("HEAD", "http://www.bKjia. c0m", false); // the server from which the time is obtained
XmlHttp. send (); // connect to the server
Var offset = Date. parse (XmlHttp. getResponseHeader ("Date"); // get the time in the header
Offset-= (new Date). getTime (); // obtain the interval between the local time and the server time
Function ShowTime () // display time
{
Var d = new Date; // obtain the current time
D. setTime (d. getTime () + offset); // obtain the current server time through the time interval between the server and the local server
Document. getElementById ('time'). innerHTML = d. toLocaleString (); // display the server time
}
SetInterval ("ShowTime ()", "1000 ");
</Script>
</Body>
</Html>


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.