One: Time-to-timestamp: JavaScript obtains timestamps in four ways, all by instantiating the time object new Date () to further obtain the current timestamp
1.var TIMESTAMP1 = date.parse (New Date ()); Results: 1477808630000 This method is not recommended and the millisecond level value is converted to 000
Console.log (TIMESTAMP1);
2.var timestamp2 = (new Date ()). ValueOf (); Results: 1477808630404 valueOf()
Accurate timestamp values are obtained by returning the original value of the specified object through the function
Console.log (TIMESTAMP2);
3.var Timestamp3 = new Date (). GetTime (); Results: 1477808630404, the millisecond value of the current time is obtained directly by the prototype method, and the accurate
Console.log (Timestamp3);
4.var timetamp4 = number (new Date ());// results: 1477808630404, converts the time to a numeric value of type A, i.e. timestamp
Console.log (Timetamp4);
The printing results are as follows:
Second, time stamp turn time
var timestamp4 = new Date (1472048779952); //Convert to current time directly with new Date (timestamp) format
Console.log (Timestamp4);
Console.log (Timestamp4.tolocaledatestring (). Replace (/\//g, "-") + "" + timestamp4.totimestring (). substr (0, 8)); Re-use splicing regularization and other means to convert to YYYY-MM-DD HH:MM:SS format
The effect is as follows:
However, this conversion in some browsers will have undesirable effect, because the tolocaledatestring () method is different from the browser, such as IE for August 24, 2016 22:26:19 format Sogou for Wednesday, August 24, 22:39:42
It can be spliced by the date of each acquisition time, for example:
function getdate () { var now = new Date (), y = now.getfullyear (), m = now.getmonth () + 1, d = now.getdate ( ); Return y + "-" + (M < 10?) "0" + m:m + "-" + (D < 10?) "0" + d:d) + "" + now.totimestring (). substr (0, 8); }
-Reprinted from Http://www.cnblogs.com/lmyt/p/6013097.html
JS time and time stamp conversion