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 ??
Asked
Active
Viewed 2.3k times
2

Ashwin
- 35
- 1
- 1
- 6
-
1`+new Date('2015-04-17 10:12:12')` - it will return a int value – Arun P Johny Apr 29 '15 at 06:16
-
I tried that when am doing it I am receiving INVALID DATE as output when console logged the above one. – Ashwin Apr 29 '15 at 06:17
-
then you will have to use a good parse like momentjs to parse the string to date then get the timestamp value from that – Arun P Johny Apr 29 '15 at 06:18
-
How about `Date.parse('2015-04-17 10:12:12')` ? See [this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse) – Raptor Apr 29 '15 at 06:19
-
[Convert a string to a Unix timestamp in javascript](http://stackoverflow.com/q/18634087/769678) – Shubh Apr 29 '15 at 06:20
-
http://jsfiddle.net/arunpjohny/3kLf4mhw/2/ - using [momentjs](http://momentjs.com/) – Arun P Johny Apr 29 '15 at 06:22
-
@arun momentjs are you refering to this link http://momentjs.com/ – Ashwin Apr 29 '15 at 06:26
-
ok thanks man let me try it out – Ashwin Apr 29 '15 at 06:29
3 Answers
8
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
:)

Abhishek
- 6,912
- 14
- 59
- 85
1
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/

Ashish Sapkale
- 540
- 2
- 13
0
You could do:
date1 = '2015-04-17 10:12:12'.split(':');
date2 = date1[0].split(' ');
date3 = date2[0].split('-');
the_date = new Date(parseInt(date3[0]),parseInt(date3[1])-1,parseInt(date3[2]),parseInt(date2[1]),parseInt(date1[1]),parseInt(date1[2]));

fcastillo
- 938
- 1
- 11
- 24
-
this does not answer OP's question. The original date format is not like this – Raptor Apr 29 '15 at 06:20
-
Well, you could transform to this format with [split](http://www.w3schools.com/jsref/jsref_split.asp) – fcastillo Apr 29 '15 at 06:23
-
ouch, that works, but it would be over-complicated. And `date3[1]` is `04` (a string) ... forget to use `parseInt()` ? – Raptor Apr 29 '15 at 06:30