The Date object for JavaScript displays the current date and time

Source: Internet
Author: User

Topic Analysis:

Use the JavaScript Date object to display the current date and time, first define a time Odate object with the new date (), and then get the value of the month, day, and hour by the Odate object:

var odate=new Date ();
var oyear=odate.getfullyear (); Get year of year
var omonth=odate.getmonth () +1; Gets the month of the month
var oday=odate.getdate (); Gets the date of the day
var ohours=odate.gethours (); Get the hour of the day
var ominute=odate.getminutes (); Get how many points at that time
var oseconds=odate.getseconds (); Get how many seconds

To connect the acquired good days and hours to the time and seconds.

var timevalue=oyear+ "-" +zero (omonth) + "-" +zero (oday) + "" +zero (ohours) + ":" +zero (Ominute) + ":" +zero (oseconds) paid Div

Obox.innerhtml=timevalue;

The zero () function inside is a 0 function, with a value less than 10 for the day of the month, plus 0 in front.

function zero (num) {
return Num>10?num: "0" +num;
}

Then run the clock () by using the SetInterval () method. It's just, every 1 seconds.

SetInterval ("clock ()", 1000);

JavaScript code:

Window.onload=function () {
Clock ();
SetInterval ("clock ()", 1000);
}
function zero (num) {
return Num>10?num: "0" +num;
}
function Clock () {
var Obox=document.getelementbyid ("date");
var odate=new Date ();
var oyear=odate.getfullyear ();
var omonth=odate.getmonth () +1;
var oday=odate.getdate ();
var ohours=odate.gethours ();
var ominute=odate.getminutes ();
var oseconds=odate.getseconds ();
var timevalue=oyear+ "-" +zero (omonth) + "-" +zero (oday) + "" +zero (ohours) + ":" +zero (Ominute) + ":" +zero (Oseconds);
Obox.innerhtml=timevalue;
}

Reprint front-end rookie: http://www.bird100.cn/8960.html

The Date object for JavaScript displays the current date and time

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.