JavaScript - 将 Date() 转换为秒 [英] JavaScript - Converting a Date() into seconds

查看:46
本文介绍了JavaScript - 将 Date() 转换为秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里使用 Algolia 制作的 Hacker News API:

I'm using the Hacker News API made at Algolia here:

https://hn.algolia.com/api

我有点困惑,因为它说要搜索帖子,因为它说要运行以下查询:

I'm a bit confused as it says to search for posts since a certain time it says to run the following query:

自时间戳 X 起的评论(以秒为单位)http://hn.algolia.com/api/v1/search_by_date?tags=comment&numericFilters=created_at_i>X

Comments since timestamp X (in second) http://hn.algolia.com/api/v1/search_by_date?tags=comment&numericFilters=created_at_i>X

它说以秒为单位用时间戳替换 X,但是你究竟会怎么做呢?假设我的最后一个帖子是 2015-08-25T15:35:58.000Z.我将如何运行此查询来搜索自该日期以来的帖子?我不知道如何将此日期转换为秒...

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...

推荐答案

getTime() 会得到以毫秒为单位的日期,所以除以 1000:

getTime() will get the date in milliseconds, so divide by 1000:

var date = new Date("2015-08-25T15:35:58.000Z");
var seconds = date.getTime() / 1000; //1440516958

这篇关于JavaScript - 将 Date() 转换为秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆