How to convert yyyy-mm-dd hh:mm:ss to unix timestamp in angularjs or javascript
19,474
Solution 1
new Date("2015/04/29 11:24:00").getTime(); //for answer in milliseconds
(new Date("2015/04/29 11:24:00").getTime()/1000); //to get answer in seconds
:)
Solution 2
You can try below snippet for unix timestamp...
var unixtimestamp = (new Date('2015-04-17 10:12:12')).getTime() / 1000;
Sorry my above solution was working fine in only Chrome.
Below soltution might help you..
var unixtimestamp = (new Date("2015-04-17 10:12:12".replace('-','/'))).getTime() / 1000;
alert(unixtimestamp);
jsfiddle link: http://jsfiddle.net/7r4c9Lqo/3/
Author by
Ashwin
Updated on November 28, 2022Comments
-
Ashwin over 1 year
I got a query how do I convert yyyy-mm-dd hh:mm:ss to UNIX time-stamp using angularjs or JavaScript for example:
2015-04-17 10:12:12
this is how my date value how do i convert ??-
Arun P Johny almost 9 years
+new Date('2015-04-17 10:12:12')
- it will return a int value -
Ashwin almost 9 yearsI tried that when am doing it I am receiving INVALID DATE as output when console logged the above one.
-
Arun P Johny almost 9 yearsthen you will have to use a good parse like momentjs to parse the string to date then get the timestamp value from that
-
Raptor almost 9 yearsHow about
Date.parse('2015-04-17 10:12:12')
? See this -
Shubh almost 9 years
-
Arun P Johny almost 9 years
-
Ashwin almost 9 years@arun momentjs are you refering to this link momentjs.com
-
Ashwin almost 9 yearsok thanks man let me try it out
-
-
Raptor almost 9 yearsthis does not answer OP's question. The original date format is not like this
-
fcastillo almost 9 yearsWell, you could transform to this format with split
-
Ashish Sapkale almost 9 yearsWorking js fiddle link, jsfiddle.net/7r4c9Lqo
-
Raptor almost 9 yearsouch, that works, but it would be over-complicated. And
date3[1]
is04
(a string) ... forget to useparseInt()
?