Real-time javascript current webpage

Source: Internet
Author: User

 

Most of the time when designing a website, you will design a time display on top of the banner. This is generally implemented using js, but some problems often occur. If you use a browser with a non-IE kernel or on a Linux system, you will find problems with the time calendar display and the year.

The following code is tested to be compatible with most browsers.

<Script language = "JavaScript">

Var myDate = new Date ();

MyDate. getYear (); // obtain the current year (2 digits)

MyDate. getFullYear (); // obtain the complete year (4 bits, 1970 -????)

MyDate. getMonth (); // obtain the current month (0-11, 0 represents January)

MyDate. getDate (); // obtain the current day (1-31)

MyDate. getDay (); // obtain X of the current week (0-6, 0 indicates Sunday)

MyDate. getTime (); // obtain the current time (milliseconds starting from 1970.1.1)

MyDate. getHours (); // obtain the current hour (0-23)

MyDate. getMinutes (); // obtain the current number of minutes (0-59)

MyDate. getSeconds (); // obtain the current number of seconds (0-59)

MyDate. getMilliseconds (); // obtain the current number of milliseconds (0-999)

MyDate. toLocaleDateString (); // obtain the current date

Var mytime = myDate. toLocaleTimeString (); // obtain the current time

MyDate. toLocaleString (); // obtain the date and time

Var week;

If (new Date (). getDay () = 0) week = "Sunday"

If (new Date (). getDay () = 1) week = "Monday"

If (new Date (). getDay () = 2) week = "Tuesday"

If (new Date (). getDay () = 3) week = "Wednesday"

If (new Date (). getDay () = 4) week = "Thursday"

If (new Date (). getDay () = 5) week = "Friday"

If (new Date (). getDay () = 6) week = "Saturday"

Document. write (myDate. toLocaleDateString () + "" + week + "" + mytime );

</SCRIPT>

 

This article is from the "Yuxuan" blog

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.