JS (get current time and expressed in 2015-01-01 format)

Source: Internet
Author: User

A simple small example to achieve the current time, the JS code is as follows:

function getdate () {
var date = new Date ();
var mon = date.getmonth () + 1; GetMonth () returns 0-11, plus 1 is required.
if (Mon <=9) {//if less than 9, you need to add 0
Mon = "0" + mon;
}
var day = Date.getdate (); GETDATE () returns 1-31, no additional 1 is required
if (day <=9) {//if less than 9, you need to add 0
Day = ' 0 ' + day;
}
document.getElementById ("Acc_time"). Value = Date.getfullyear () + "-" + Mon + "-" + day;
}

Here is the HTML code:

<input id= "Acc_time" class= "Form-control" name= "Acc_time" type= "text" onchange= "getdate ()" onkeyup= "getdate ()"/ >

OnChange () and onkeyup () Two events are intended to allow the user to change the contents of a text box.

Notes (get other date formats):

var date = new Date ();
Date.getyear ();       //Get Current year (2-bit)
Date.getfullyear ();    //Get the full year (4-bit, 1970-????)
Date.getmonth ();      //Get the current month (0-11, 0 for January)
Date.getdate ();        //Get current day (1-31)
Date.getday ();        / /Get Current week x (0-6, 0 for Sunday)
Date.gettime ();       //Get current time (number of milliseconds from 1970.1.1 onwards)
Date.gethours ();      //Get current hours (0-23)
Date.getminutes ();     //Gets the current number of minutes (0-59)
Date.getseconds ();    //Gets the current number of seconds (0-59)
Date.getmilliseconds ();    //Gets the current number of milliseconds (0-999)
date.tolocaledatestring ();    //Get current date
Var mytime= Date.tolocaletimestring ();    //Get current time
Date.tolocalestring ();        //Get date and time

JS (Gets the current time and is represented in the 2015-01-01 format)

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.