JavaScript gets current time function and time operation summary _javascript tips

Source: Internet
Author: User
Tags getdate

In the project need a timer, the effect is as follows:

JS Code:

Copy Code code as follows:

/* Get current time * *
function Getcurrentdate ()
{
var timestr = ';
var curdate = new Date ();
var curmonth = Curdate.getmonth () +1; Get Current month (0-11, 0 for January)
var curday = Curdate.getdate (); Get the current day (1-31)
var curweekday = Curdate.getday (); Get Current week x (0-6, 0 for Sunday)
var curhour = curdate.gethours (); Get the current number of hours (0-23)
var curminute = Curdate.getminutes (); Get the current number of minutes (0-59)
var cursec =curdate.getseconds (); Get the current number of seconds (0-59)
Timestr = curmonth+ ' month ' +curday+ ' Day week ';
Switch (curweekday)
{
Case 0:timestr + = ' Day ';
Case 1:timestr = ' one ';
Case 2:timestr + = ' two ';
Case 3:timestr + = ' three ';
Case 4:timestr + = ' four ';
Case 5:timestr + = ' five ';
Case 6:timestr + = ' six ';
}
if (Curhour < 10)
{
if (Curminute < 10)
{
if (Cursec < 10)
{
Timestr + = ' 0 ' +curhour+ ': 0 ' +curminute+ ': 0 ' +cursec;
}
Else
{
Timestr + = ' 0 ' +curhour+ ': 0 ' +curminute+ ': ' +cursec;
}
}
Else
{
if (Cursec < 10)
{
Timestr + = ' 0 ' +curhour+ ': ' +curminute+ ': 0 ' +cursec;
}
Else
{
Timestr = ' 0 ' +curhour+ ': ' +curminute+ ': ' +cursec;
}
}
}
Else
{
if (Curminute < 10)
{
if (Cursec < 10)
{
Timestr + + ' +curhour+ ': 0 ' +curminute+ ': 0 ' +cursec;
}
Else
{
Timestr + = ' +curhour+ ': 0 ' +curminute+ ': ' +cursec;
}
}
Else
{
if (Cursec < 10)
{
Timestr + = ' +curhour+ ': ' +curminute+ ': 0 ' +cursec;
}
Else
{
Timestr = ' +curhour+ ': ' +curminute+ ': ' +cursec;
}
}
}
$ ("#time"). Text (TIMESTR);
}

Then the bar uses this function on the line.

Finally, make a summary of some of the functions of JavaScript dates:

var mydate = new Date ();

Mydate.getyear (); Get Current year (2-bit)

Mydate.getfullyear (); Get full year (4-bit, 1970-????)

Mydate.getmonth (); Get Current month (0-11, 0 for January)

Mydate.getdate (); Get the current day (1-31)

Mydate.getday (); Get Current week x (0-6, 0 for Sunday)

Mydate.gettime (); Gets the current time (number of milliseconds since 1970.1.1)

Mydate.gethours (); Get the current number of hours (0-23)

Mydate.getminutes (); Get the current number of minutes (0-59)

Mydate.getseconds (); Get the current number of seconds (0-59)

Mydate.getmilliseconds (); Get current number of milliseconds (0-999)

Mydate.tolocaledatestring (); Get Current date

var mytime=mydate.tolocaletimestring (); Get current time

Mydate.tolocalestring (); Get 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.