JS Get time new Date () detailed introduction __js

Source: Internet
Author: User
Tags set time

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)//So get current month is mydate.getmonth () +1;
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

==========================================================================

JS Gets the current timestamp method-javascript The following three methods are available for obtaining the current millisecond timestamp:

var timestamp =date.parse (New Date ()); Results: 1280977330000//Not recommended; Milliseconds changed to 000 display

var timestamp = (new Date ()). valueof (); Results: 1280977330748//recommended;

var timestamp=new Date (). GetTime (); Results: 1280977330748//recommended;

JS to call the new Date () separately; Show this format 10:10:43 utc+0800 2012

But using the new Date () to participate in the calculation is automatically converted to the number of milliseconds starting from 1970.1.1

--------------------------------------------------------------------------------------------------

converts a date in the form of a string to a Date object

var strtime= "2011-04-16"; String date format
var date= new Date (Date.parse (Strtime.replace (/-/g, "/")); Convert to data ();

--------------------------------------------------------------------------------------------------

New Date (); parameter can be an integer; can also be a string; But the format must be correct

New Date (2009,1,1); That's right

New Date ("2009/1/1"); That's right

New Date ("2009-1-1"); Error

New Date (year, month, date, hrs, min, sec) Create a Date object by the given parameters
Parameter description:
Year's value is: the date that needs to be set-1900. For example, the year should be set at 1997, and the value should be 97, or 1997-1900. So date can be set to a minimum of 1900 years;
Month's range is 0~11,0 representative January, 11 table represents December;
The domain of date is between 1~31;
The range of hrs is between 0~23. From midnight to 1 o'clock in the morning hrs=0, between noon and 1 o'clock in the afternoon hrs=12;
The range between min and sec is between 0~59.
Example Date day=new date (11,3,4);
The time in day is: 04-apr-11 12:00:00 AM


In addition, you can also give incorrect parameters.
Example set time for February 30, 1910, it will be interpreted as March 2.
Date Day=new date (10,1,30,10,12,34);
System.out.println ("Day's date is:" +day);
The result is: Day ' s date is:web 10:13:34 gmt+08:00 1910------------------------------------------------------------- -------------------------------------

1. Current system locale format (tolocaledatestring and tolocaletimestring)

Example: (new Date ()). toLocaleDateString () + "" + (new Date ()). toLocaleTimeString ()
Results: January 29, 2008 16:13:11

2. Ordinary strings (todatestring and toTimeString)

Example: (new Date ()). toDateString () + "" + (new Date ()). toTimeString ()
Results: Tue 2008 16:13:11 utc+0800

3. Greenwich Standard Time (togmtstring)

Example: (New Date ()). toGMTString ()
Result: Tue, 2008 08:13:11 UTC

4. Global Standard Time (toutcstring)

Example: (New Date ()). toUTCString ()
Result: Tue, 2008 08:13:11 UTC

5.Date Object String (toString)

Example: (New Date ()). ToString ()
Result: Tue 16:13:11 utc+0800 2008

--------------------------------------------------------------------------------------------------

Date objects can also be used to compare two dates. var mydate=new Date ();      Mydate.setfullyear (2008,7,9);      var today = new Date ();       if (mydate>today) {...; }


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.