How to handle JWT.exp time? [duplicate] - javascript

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

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

Convert UTC date seconds to Date [duplicate]

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

Equivalent code in python (time) [duplicate]

This question already has answers here:
Converting unix timestamp string to readable date
(19 answers)
Closed 6 years ago.
Javascript code:
var date = new Date(1466278504960);
return: Sat Jun 18 2016 20:35:04 GMT+0100 (WEST)
How can I convert the same number to date in python ?
When I use
datetime.datetime.fromtimestamp(int("1466278504960")).strftime('%Y-%m-%d %H:%M:%S'))
I receive this error: ValueError: year is out of range
datetime.datetime.fromtimestamp will do this, but you need to divide the value by 1000 first (the numeric value you give and JavaScript's Date expects is in milliseconds since the epoch, where Python's API takes a floating point seconds since the epoch):
from datetime import datetime
date = datetime.fromtimestamp(1466278504960 / 1000.)
That makes the raw datetime object; if you want it formatted the same, you should take a look at datetime object's strftime method.
It's almost the same. You just have to convert the units.
Date from javascript specifies the number in milliseconds, in other words, expects a number in millisenconds as a parameter. When the python date takes seconds.

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

how do I convert date to milliseconds [duplicate]

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

Categories