This question already has answers here:
Javascript Convert Date Time string to Epoch
(12 answers)
Closed 7 years ago.
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...
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
Related
This question already has answers here:
Using PHP to Match a Javascript Timestamp
(3 answers)
Closed 3 years ago.
In JavaScript I am using (new Date("1985-05-01")).getTime(); to get the timestamp. The return value is 483753600000.
In PHP I would like to return the year of that timestamp.
date('Y', 483753600000); returns 17299 but 1985 is expected.
What am I doing wrong?
JavaScript's getTime() will return timestamp in miliseconds. PHP's date() function needs timestamp in seconds. So just divide it by 1000:
date('Y', 483753600000 / 1000);
This question already has answers here:
Convert a Unix timestamp to time in JavaScript
(34 answers)
Closed 4 years ago.
console.log('DEBUG::+jwtDecode(token).exp', +jwtDecode(token).exp); //1534820211
console.log('DEBUG::try', new Date(+jwtDecode(token).exp).toISOString());
//DEBUG::try 1970-01-18T18:20:20.211Z
I'm having a token with value 1534820211 and when I try to convert it using toISOString() it gives me year 1970-01-18T18:20:20.211Z.
But when I decode the same token at jwt.io, and mouse hover over exp, it shows 2018-08-21.... which is huge difference. I have also tried to pass jwtDecode(token).exp into moment and using format, still return me datetime in 1970xxxx.
moment(jwtDecode(token).exp).format();
The value you have is seconds from epoch.
JavaScript Date constructor (and moment function too) accepts value in milliseconds from epoch. Multiply the number by 1000, and your code should work fine:
var exp = 1534820211 * 1000;
console.log(new Date(exp));
This question already has answers here:
Convert a Unix timestamp to time in JavaScript
(34 answers)
Closed 5 years ago.
Pretty straightforward issue, but I haven't found any information on this after looking around a bunch.
Essentially, I want to convert a series of UTC dates (e.g. "1505952000") into regular date strings (e.g., "9/21"), to use today as an example.
For some reason, however, .toDateString() is erroring out as "not a function" when I try to run it. How do I make this simple conversion work?
Here's my code, and I've console-logged day.dt to ensure that it's a valid UTC date when it runs:
let dt = day.dt.toDateString();
UTC var stored in seconds from Jan. 1, 1970.
So to convert it back to the local date time, use this snippet:
var d = new Date(0);
d.setUTCSeconds(1505952000);
console.log(d);
OR
var d = new Date(1505952000 * 1000); // Because this constructor takes miliseconds.
console.log(d);
This question already has answers here:
Get time difference between two dates in seconds
(10 answers)
Closed 7 years ago.
Lets say I have a date-time object 2015-12-31T12:59. Is there a way in JavaScript to find the difference between current date-time and the above date-time object in seconds? Basically, is there a way to find out the time in seconds from this very moment till the date-time specified by a future date-time object?
I did some digging but couldn't find anything that could be of any use for me in this case.
To achieve this you can simply subtract the date object from Date.now(). Then take the millisecond value that it gives you, and divide by 1000 to get the second value. Here is a live example:
var date1 = new Date("2015-12-31T12:59");
var date2 = Date.now();
document.getElementById("output").innerHTML = (date1 - date2) / 1000;
Seconds between now and (2015-12-31T12:59): <span id="output"></span>
This question already has answers here:
Calculating milliseconds from epoch
(5 answers)
Closed 8 years ago.
I have a string the represents a time "2014-07-03T11:47:00"
I would like to covert this time to milliseconds and I think it should look something like the following.
var myTime = Date.parse("2014-07-03T11:47:00");
var myTimeMs = myTime.getTime();
Any suggestions about how to get the value in milliseconds on any browser?
Date.parse returns the time in milliseconds
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse