JS Code for displaying the current date and time

Source: Internet
Author: User

JS display current date

Copy codeThe Code is as follows:
<Script language = "Javascript">
Var datelocalweek = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday ");
Var datelocalnow = new Date ();
Var datelocalyear = datelocalnow. getFullYear ();
Var datelocalmonth = (datelocalmonth = "0" + (datelocalnow. getMonth () + 1). substr (datelocalmonth. length-2, 2 );
Var datelocalday = (datelocalday = "0" + datelocalnow. getDate (). substr (datelocalday. length-2, 2 );
Var datelocalweekday = datelocalweek [datelocalnow. getDay ()];
Document. write (datelocalyear + "year" + datelocalmonth + "month" + datelocalday + "day" + "" + datelocalweekday );
</Script>


The effect is as follows:

The corresponding JS is as follows:
Copy codeThe Code is as follows:
// Create an array to store the number of days per month
Function montharr (m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11 ){
This [0] = m0;
This [1] = m1;
This [2] = m2;
This [3] = m3;
This [4] = m4;
This [5] = m5;
This [6] = m6;
This [7] = m7;
This [8] = m8;
This [9] = m9;
This [10] = m10;
This [11] = m11;
}
// Implement the calendar
Function calendar (){
Var monthNames = "JanFebMarAprMayJunJulAugSepOctNovDec ";
Var today = new Date ();
Var thisDay;
Var monthDays = new montharr (31, 28, 31, 30, 31, 30, 31, 31, 30, 31 );
Year = today. Fig () + 1900;
ThisDay = today. getDate ();
If (year % 4 = 0) & (year % 100! = 0) | (year % 400 = 0 ))
MonthDays [1] = 29;
NDays = monthDays [today. getMonth ()];
FirstDay = today;
FirstDay. setDate (1 );
TestMe = firstDay. getDate ();
If (testMe = 2)
FirstDay. setDate (0 );
StartDay = firstDay. getDay ();
Var dayNames = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday ");
Var monthNames = new Array ", "August September", "August October", "August November", "August December ");
Var now = new Date ();
Document. write ("<div id = 'rili' style = 'position: absolute; width: 140px; left: 200px; top: 50px; '> ")
Document. write ("<TABLE width = '000000' BORDER = '0' CELLSPACING = '0' CELLPADDING = '2' BGCOLOR = '# 0080FF'> ")
Document. write ("<TR> ");
Document. write ("<TD> ");
Document. write ("<table border = '0' cellspacing = '1' cellpadding = '2' bgcolor = 'silver '> ");
Document. write ("<TR> <th colspan = '7' bgcolor = '# C8E3FF'> ");
Document. writeln ("<font style = 'font-size: 9pt; Color: #330099 '>" + "ad" + now. getYear () + "year" + monthNames [now. getMonth ()] + "" + now. getDate () + "day" + dayNames [now. getDay ()] + "</FONT> ");
Document. writeln ("</TH> </TR> <th bgcolor = '# 0080FF'> <font style = 'font-size: 9pt; Color: white'> day </FONT> </TH> ");
Document. writeln ("<th bgcolor = '# 0080FF'> <font style = 'font-size: 9pt; Color: white'> 1 </font> </TH> ");
Document. writeln ("<th bgcolor = '# 0080FF'> <font style = 'font-size: 9pt; Color: white'> 2 </font> </TH> ");
Document. writeln ("<th bgcolor = '# 0080FF'> <font style = 'font-size: 9pt; Color: white'> 3 </font> </TH> ");
Document. writeln ("<th bgcolor = '# 0080FF'> <font style = 'font-size: 9pt; Color: white'> 4 </font> </TH> ");
Document. writeln ("<th bgcolor = '# 0080FF'> <font style = 'font-size: 9pt; Color: white'> 5 </font> </TH> ");
Document. writeln ("<th bgcolor = '# 0080FF'> <font style = 'font-size: 9pt; Color: white'> 6 </font> </TH> ");
Document. writeln ("</TR> <TR> ");
Column = 0;
For (I = 0; I <startDay; I ++ ){
Document. writeln ("\ n <TD> <font style = 'font-size: 9pt '> </font> </TD> ");
Column ++;
}

For (I = 1; I <= nDays; I ++ ){
If (I = thisDay ){
Document. writeln ("</TD> <td align = 'center' BGCOLOR = '# FF8040'> <font style = 'font-size: 9pt; Color: # ffff'> <B> ")
}
Else {
Document. writeln ("</TD> <td bgcolor = '# ffff' ALIGN = 'center'> <font style = 'font-size: 9pt; font-family: Arial; font-weight: bold; Color: #330066 '> ");
}
Document. writeln (I );
If (I = thisDay)
Document. writeln ("</FONT> </TD> ")
Column ++;
If (column = 7 ){
Document. writeln ("<TR> ");
Column = 0;
}
}
Document. writeln ("<TR> <td colspan = '7' ALIGN = 'center' VALIGN = 'top' BGCOLOR = '# 0080FF'> ")
Document. writeln ("<form name = 'clock 'onSubmit = '0'> <font style = 'font-size: 9pt; Color: # ffffff'> ")
Document. writeln ("Current Time: <input type = 'text' NAME = 'face' ALIGN = 'top'> </FONT> </FORM> </TD> </TR> </TABLE> ")
Document. writeln ("</TD> </TR> </TABLE> </div> ");
}

Var timerID = null;
Var timerRunning = false;

Function stopclock (){
If (timerRunning)
ClearTimeout (timerID );
TimerRunning = false;
}

// Display the current time
Function showtime (){
Var now = new Date ();
Var hours = now. getHours ();
Var minutes = now. getMinutes ();
Var seconds = now. getSeconds ()
Var timeValue = "" + (hours> 12 )? Hours-12: hours)
TimeValue + = (minutes <10 )? ": 0": ":") + minutes
TimeValue + = (seconds <10 )? ": 0": ":") + seconds
TimeValue + = (hours> = 12 )? "Afternoon": "morning"
Document. clock. face. value = timeValue;
TimerID = setTimeout ("showtime ()", 1000); // you can specify a timeout value to display the time dynamically.
TimerRunning = true;
}

Function startclock (){
Stopclock ();
Showtime ();
}

The usage page is displayed as follows:
Copy codeThe Code is as follows:
<Html>
<Script language = "JavaScript" type = "text/javascript" src = "time. js"> </script>
<Body bgcolor = "# FFFFFF" onLoad = "startclock (); timerONE = window. setTimeout" text = "#000000">
<Script language = "JavaScript" type = "text/javascript">
Calendar ();
</Script>
</Body>
</Html>

This is OK!

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.