Get the timestamp of the current time in JavaScript
Method One:
var timestamp=date.parse (new Date ()); = = = = 1451441086000 Result (s):
Note : This method is accurate to the second, and the millisecond position is replaced with 0.
Method Two:
var timestamp= (new Date ()). ValueOf (); = = = = 1451441232779 Result (s):
Note: Both methods get the number of milliseconds that start at midnight on January 1, 1970
Method Three:
Using the new Date (). GetTime () method in JavaScript , use theDate.now () method directly with IE8 or later
IE8 or later
if (! Date.now) {
Date.now = function () {return new Date (). GetTime ();};
}
JQuery gets timestamp $.now () var timestamp = $.now ();
Ii. jquery converts specific time into timestamps
1. Introduction of Jquery.extend.date.js plug-in
<script type= "Text/javascript" src= ". /js/jquery.extend.date.js "></script>
2. Code principle
(function ($) {$.extend ({myTime: {/** * current timestamp * @return <int> Unix timestamp (seconds) */curtime:function () {return date.parse (new Date ())/1000; },/** * Date converted to UNIX timestamp * @param <string> 2014-01-01 20:20:20 Date grid Type * @return <int> Unix Timestamp (SEC) */Datetounix:fu Nction (String) {var f = string.split (', 2); var d = (f[0]? F[0]: '). Split ('-', 3); var t = (f[1]? F[1]: '). Split (': ', 3); Return (New Date (parseint (d[0), ten) | | null, (parseint (d[1], 10) | | 1)-1, parseint (D[2], 10) | | NULL, parseint (t[0], 10) | | NULL, parseint (t[1], 10) | | NULL, parseint (t[2], 10) | | Null)). GetTime ()/1000; },/** * Timestamp conversion date * @param <int> unixtime Time stamp (sec) * @param <bool> Isfull return full time (y-m-d or y-m-d h:i:s) * @param & lt;int> TimeZone Time Zone */unixtodate:function (unixtime, Isfull, TimeZone) { if (typeof (timeZone) = = ' number ') {unixtime = parseint (unixtime) + parseint ( TimeZone) * 60 * 60; } var time = new Date (unixtime * 1000); var ymdhis = ""; Ymdhis + = Time.getutcfullyear () + "-"; Ymdhis + = (time.getutcmonth () +1) + "-"; Ymdhis + = Time.getutcdate (); if (Isfull = = = True) {Ymdhis + = "" + time.getutchours () + ":"; Ymdhis + = time.getutcminutes () + ":"; Ymdhis + = Time.getutcseconds (); } return ymdhis; } } });}) (JQuery);
3. Application
$.mytime. Datetounix (' 2015-12-30 12:30:15 '); The date is converted to timestamp $.mytime. Unixtodate (1451442143254); Time Stamp converted to date
Front End (js/jquery) conversion of date and time stamps