Is it possible in javascript to convert some date to a timestamp?
I have a date in this format 2010-03-09 12:21:00 and I want to convert it to its equivalent timestamp using javascript.
2010-03-09 12:21:00
In response to your edit:
You need to parse a date string to build a Date object, and then you can get a timestamp, for example:
Date
function getTimestamp(str) { var d = str.match(/\d+/g); // extract date parts return +new Date(d[0], d[1] - 1, d[2], d[3], d[4], d[5]); // build Date object } getTimestamp("2010-03-09 12:21:00"); // 1268158860000
In the above function, I use a simple regular expression to extract numbers, then I build a new Date object using a date constructor with this (Note: The Date object treats months as 0 based numbers, for example Jan-0, Feb-1, .. ., 11-Dec).
Then I use the unary plus operator to get the timestamp.
Note that the timestamp is expressed in milliseconds.
+(new Date())
Is the work in progress.
The getTime() method of instances of a Date object returns the number of milliseconds since an era; which is a pretty good timestamp.
getTime()
Source: https://habr.com/ru/post/1303530/More articles:As an internal expando object - .netSwitching views in ViewFlipper - androidOAuth on iPhone: using Safari or UIWebView? - iphoneDjango: How to iterate over forms and access cleared data? - djangoJavascript equivalent of Python iterkeys () method - javascriptCan you publish .war directly from eclipse to a web server? - javajQuery.eq (x) returns a different element in IE than in FF / Chrome - jquery37signals layout template - htmlhg: how to convert a single subversion branch - version-controlExcluding some tags from the Cufon Font system - classAll Articles