Get time in JavaScript new date () detail

Source: Internet
Author: User

Original: Get time in JavaScript new date () detail

17javaScript Tutorial Series: JS get time new Date () detailed introduction



JS Time Newdate () character type conversion

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

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

JS Gets the current timestamp method-javascript gets the current timestamp

JavaScript Gets the current timestamp:
The first method:


var timestamp =date.parse (New Date ());
Results: 1280977330000
The second method:

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

The third method:

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

the first: the timestamp obtained is to change the milliseconds to 000 display,

The second and third are to obtain the timestamp of the current millisecond.


I and my colleagues in the use of JS to show the analysis of the data left the approximate time of the process, time is always changed to 0, the result is very strange, and finally found that the time to obtain the Date.parse (Newdate ()) to obtain the time stamp to change the millisecond to 000 display, so the difference between the calculation of inaccurate.

You can use the second or third method to calculate the time difference.

In JS, call new Date () Separately, such as document.write (new Date ());

The result displayed is: 10:10:43 utc+0800 2012 time in this format

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 ();


var month=date.getmonth () +1; Get Current Month


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.