Js get the current time displayed on the page and refresh every second _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces how to get the current time of js and display it on the page and refresh it every second. If you need it, you can refer to the method below. The Code is also very concise. just provide the Code directly.

The Code is as follows:






Js retrieves the current time and displays it on the page
Script
Window. onload = function (){
// The timer calls fnDate once per second ()
SetInterval (function (){
FnDate ();
},1000 );
}
// Obtain the current time in js
Function fnDate (){
Var oDiv = document. getElementById ("p1 ");
Var date = new Date ();
Var year = date. getFullYear (); // current year
Var month = date. getMonth (); // current month
Var data = date. getDate (); // day
Var hours = date. getHours (); // hour
Var minute = date. getMinutes (); // minute
Var second = date. getSeconds (); // second
Var time = year + "-" + fnW (month + 1) + "-" + fnW (data) + "" + fnW (hours) + ": "+ fnW (minute) +": "+ fnW (second );
ODiv. innerHTML = time;
}
// Fill in 0 when a field is not two digits
Function fnW (str ){
Var num;
Str> 10? Num = str: num = "0" + str;
Return num;
}
Script




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.