JavaScript series: JS get time new Date () detailed description

Source: Internet
Author: User

JavaScript series: JS get time new Date () detailed description

automatically get today's Day of the week:
var aaa = ' Day 123456 '
Aaa.charAt(New Date (). GetDay ()) "Day "Mydate.getfullyear () + ' year ' + (Mydate.getmonth () +1) + ' month ' +mydate.getdate () + ' Day ' "November 8, 2015 "

var mydate = new Date ();
Mydate.getyear (); Get Current year (2-bit)
Mydate.getfullyear (); Get the full year (4-bit, 1970-????)
mydate.getmonth ();//Get the current month (0-11, 0 for January) mydate.getmonth (+1)
Mydate.getdate (); Get current day (1-31)
Mydate.getfullyear () + ' year ' + (Mydate.getmonth () +1) + ' month ' +mydate.getdate () + ' Day ' "November 8, 2015"

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

mydate.gettime ();//Gets the current time (the number of milliseconds since 1970.1.1) time stamp

Mydate.gethours (); Get current number of hours (0-23)
Mydate.getminutes (); Gets the current number of minutes (0-59)
Mydate.getseconds (); Gets the current number of seconds (0-59)
Mydate.getmilliseconds (); Gets the current number of milliseconds (0-999)
mydate.tolocaledatestring ();//Get the current date "2015/10/9"
var mytime=mydate.tolocaletimestring ();//Get Current time "Afternoon 4:15:47"
mydate.tolocalestring ();//Get Date and time "2015/10/9 pm 4:15:47"

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

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 time stamp obtained is the change of milliseconds to 000 display,

The second and third is to get the timestamp of the current millisecond.

I and colleagues in the use of JS to achieve a display analysis of the time remaining in the data, the time is always changed to 0, the result is very strange, and finally found that the time of acquisition using Date.parse (Newdate ()) to obtain the time stamp to change the millisecond to 000 display, so the difference is not accurate calculation.

The time difference can be calculated using the second or third method.

JS, call the new date () Separately, for example document.write (new Date ());

Displaying the result: Mar 10:10:43 utc+0800 2012 time in this format

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

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

Convert a date in string form 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

JavaScript series: JS get time new Date () detailed description

Related Article

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.