如何在 angularjs 或 javascript 中将 yyyy-mm-dd hh:mm:ss 转换为 unix 时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29935730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:21:00  来源:igfitidea点击:

How to convert yyyy-mm-dd hh:mm:ss to unix timestamp in angularjs or javascript

javascriptangularjs

提问by Ashwin

I got a query how do I convert yyyy-mm-dd hh:mm:ss to UNIX time-stampusing angularjs or JavaScriptfor example:2015-04-17 10:12:12this is how my date value how do i convert ??

我收到一个查询,例如如何使用angularjs 或 JavaScript将 yyyy-mm-dd hh:mm:ss 转换为UNIX 时间戳:这就是我的日期值如何转换?2015-04-17 10:12:12

回答by Abhishek

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

:)

:)

回答by Ashish Sapkale

You can try below snippet for unix timestamp...

您可以尝试使用以下代码段获取 unix 时间戳...

var unixtimestamp = (new Date('2015-04-17 10:12:12')).getTime() / 1000;

Sorry my above solution was working fine in only Chrome.

抱歉,我的上述解决方案仅在 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/

jsfiddle 链接:http: //jsfiddle.net/7r4c9Lqo/3/

回答by fcastillo

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]));