JavaScript - Converting a Date() into seconds [duplicate]
Asked Answered
R

1

35

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:

Comments since timestamp X (in second) 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...

Rockling answered 25/8, 2015 at 15:48 Comment(0)
K
91

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
Kate answered 25/8, 2015 at 15:50 Comment(3)
in some cases, you may want to add rounding. Math.round(...)Kame
@IonicMan, welcome to SO. What did you try, what did you expect?Irizarry
Math.round(Date.now() / 1000) - shorter and about twice as performantAgadir

© 2022 - 2024 — McMap. All rights reserved.