JavaScript - 将 Date() 转换为秒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32208476/
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
JavaScript - Converting a Date() into seconds
提问by germainelol
I'm using the Hacker News API made at Algolia here:
我在这里使用 Algolia 制作的 Hacker News API:
I'm a bit confused as it says to search for posts since a certain time it says to run the following query:
我有点困惑,因为它说要搜索帖子,因为它说要运行以下查询:
Comments since timestamp X
(in second)
http://hn.algolia.com/api/v1/search_by_date?tags=comment&numericFilters=created_at_i>X
自时间戳记起的评论X
(以秒为单位)
http://hn.algolia.com/api/v1/search_by_date?tags=comment&numericFilters=created_at_i>X
It says to replace X
with a timestamp in seconds, but how exactly would you do this? Let's say the last post I have is at 2015-08-25T15:35:58.000Z
. How exactly would I run this query to search for posts since that date? I don't know how to convert this date to seconds...
它说要X
以秒为单位替换时间戳,但是您究竟会如何做到这一点?假设我的最后一个帖子是在2015-08-25T15:35:58.000Z
. 我将如何运行此查询来搜索自该日期以来的帖子?我不知道如何将此日期转换为秒...
Thanks in advance!
提前致谢!
回答by tymeJV
getTime()
will get the date in milliseconds, so divide by 1000:
getTime()
将获得以毫秒为单位的日期,因此除以 1000:
var date = new Date("2015-08-25T15:35:58.000Z");
var seconds = date.getTime() / 1000; //1440516958