Javascript to get the current date and time

Source: Internet
Author: User

Javascript to get the current date and time
Sometimes there may be such a requirement, so that you can view the date by time. I used to steal all my handwriting. I wrote it myself when I was bored today, which is quite simple. The time is calculated every second. Demo


The following is the Html code:
<! DOCTYPE html>
<Html class = "no-js">
<Head>
<Meta charset = "UTF-8">
<Title> current time </title>
<Script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type = "text/javascript"> </script>
<Style type = "text/css">
. Date {height: 100px; line-height: 100px; margin: 0 auto; font-family: Arial, Helvetica, sans-serif; font-size: 40px;
Border: solid 1px # ccc; box-shadow: 0 0 3px rgba (0,0, 0, 0.3) inset; border-radius: 5px; text-shadow: 1px 1px 2px rgba (0.3, 0 );}
. Date strong {padding: 0 10px; display: inline-block; height: 100px; border-right: solid 1px # ccc ;}
</Style>
</Head>
<Body>
<H1> js simply obtains the current date and time <P class = "date">
<Strong id = "Y"> </strong>
<Strong id = "MH"> </strong>
<Strong id = "TD"> </strong>
<Strong id = "D"> </strong>
<Strong id = "H"> </strong>
<Strong id = "M"> </strong>
<Strong id = "S"> </strong>
</P>
<Script type = "text/javascript">
$ (Document). ready (function (){
SetInterval (showTime, 1000 );
Function timer (obj, txt ){
Obj. text (txt );
}
Function showTime (){
Var today = new Date ();
Var weekday = new Array (7)
Weekday [0] = "Monday"
Weekday [1] = "Tuesday"
Weekday [2] = "weekday"
Weekday [3] = "Thursday"
Weekday [4] = "Friday"
Weekday [5] = "Saturday"
Weekday [6] = "Sunday"
Var y = today. getFullYear () + "year ";
Var month = today. getMonth () + "month ";
Var td = today. getDate () + "day ";
Var d = weekday [today. getDay ()];
Var h = today. getHours ();
Var m = today. getMinutes ();
Var s = today. getSeconds ();
Timer ($ ("# Y"), y );
Timer ($ ("# MH"), month );
Timer ($ ("# TD"), td );
Timer ($ ("# D"), d );
Timer ($ ("# H"), h );
Timer ($ ("# M"), m );
Timer ($ ("# S"), s );
}
})
</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.