how do I convert date to milliseconds [duplicate] - javascript

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

Related

date() is not returning the expected value [duplicate]

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

How to handle JWT.exp time? [duplicate]

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

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

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

convert Javascript timestamp into UTC format [duplicate]

This question already has answers here:
How do I get a UTC Timestamp in JavaScript?
(16 answers)
Closed 7 years ago.
For example if you receive a timestamp in Javascript:
1433454951000
How would you create a function to convert the timestamp into UTC like:
2015/6/4 GMT+7
var d1 = new Date("UNIX TIME STAMP HERE");
d1.toUTCString()
Originally asked here:
How do I get a UTC Timestamp in JavaScript?

How can I convert milliseconds to date and time string? [duplicate]

This question already has answers here:
How to convert milliseconds into a readable date?
(7 answers)
Closed 9 years ago.
can I convert milliseconds to date and time string. suppose I have millisecond in a long number and I want it like this:
Oct 22 2013 09:50:17
is this possible??
Like this:
var date = new Date(1324339212260);
date.toString("MMM dd");

Categories