A comprehensive introduction to Get Time new Date of in js

  • 2021-06-29 09:48:55
  • OfStack

var myDate = new Date();

myDate.getYear(); //Get the current year (2 digits)

myDate.getFullYear(); //Get the full year (4 digits, 1970-?? ? ?)

myDate.getMonth(); //Get the current month (0-11,0 for January)

myDate.getDate(); //Get the current day (1-31)

myDate.getDay(); //Get the question of the current week X (0-6,0 for Sunday)

myDate.getTime(); //Get the current time (milliseconds since 1970.1.1)

myDate.getHours(); //Get the current hour (0-23)

myDate.getMinutes(); //Get the current minute (0-59)

myDate.getSeconds(); //Get the current number of seconds (0-59)

myDate.getMilliseconds(); //Get current milliseconds (0-999)

myDate.toLocaleDateString(); //Get the current date

var mytime=myDate.toLocaleTimeString(); //Get the current time

myDate.toLocaleString(); //Get date and time

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

JS Get Current Timestamp Method-JavaScript Get Current Timestamp

JavaScript gets the current timestamp:

Method 1:

var timestamp =Date.parse(new Date());
Result: 1280977330000

Method 2:

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

Method 3:

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

First, get a timestamp that changes milliseconds to 1000 displays.

The second and third are to get the current millisecond timestamp.

My colleagues and I used js to implement a process that showed the approximate amount of time left in the analysis data, and the time always changed to 0. The results were bizarre. Finally, I found that the time stamp obtained by Date.parse (newDate()) changed milliseconds to 1000 displays, so the time difference calculation was not accurate.

The time difference can be calculated in either the second or third method.http://hovertree.com/menu/javascript/

new Date () is called separately in js, for example, document.write (new Date ());

The result is: time in Mar 31 10:10:43 UTC+0800 2012 format

However, participating in the calculation with new Date () automatically converts to milliseconds starting at 1970.1.1

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

Converts 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 the current month


Related articles: