Equivalent code in python (time) [duplicate] - javascript

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.

Related

gave same time on converting Date.now() [duplicate]

This question already has answers here:
Convert a Unix timestamp to time in JavaScript
(34 answers)
Closed 2 years ago.
I'm looking for something to convert exact time
var arr = [1585287883,1585287876,1585287736,1585287730,1585287725,1585287720];
arr.forEach(val=>{
console.log(Date(val).toString())
})
There are 2 issues with what you posted:
Date -> new Date
The timestamps in javascript should represent milliseconds, currently they represent seconds. You can multiply the timestamps by 1000 when building the date object, to convert seconds to milliseconds.
var arr = [1585287883,1585287876,1585287736,1585287730,1585287725,1585287720];
arr.forEach(val=>{
console.log(new Date(val*1000).toString())
})
The fact is that you're using the wrong input format. Instead of seconds since 1970-01-01, you should use milliseconds.
A JavaScript date is fundamentally specified as the number of
milliseconds that have elapsed since midnight on January 1, 1970, UTC.
This date and time is the same as the UNIX epoch, which is the
predominant base value for computer-recorded date and time values.
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Simply, multiply you're input by 1000 (and use the keyword new ;) )
var arr = [1585287883,1585287876,1585287736,1585287730,1585287725,1585287720];
arr.forEach(val=>{
console.log(new Date(val * 1000).toString());
})

How to convert long date value to string format in JavaScript [duplicate]

This question already has answers here:
Converting Unix timestamp to Date Time String [duplicate]
(2 answers)
How do I format a date in JavaScript?
(68 answers)
Closed 3 years ago.
I want to know how to convert a long date like this 1542814586896 into a String format like this 2019/02/05
You can use Date class for setting time in integer format and getting any values like day, month, year
let date = new Date(1542814586896);
console.log(date.getDay(), date.getMonth(), date.getFullYear())
You can use
new Date(1542814586896).toLocaleDateString(`ja-JP`);
//-> "2018/11/21"
.toLocaleDateString() formats time into a format of a specific region. In the example above, time's formatted into Japanese format (just because it seems like in Japan they use exactly the format you need).
What's cool about this method is that you may just pass no argument to toLocaleDateString & it will then just automatically pick the format that the final user prefers (or more precisely, the format that is set in user's OS).
For example in my browser:
new Date(1542814586896).toLocaleDateString();
//-> "21/11/2018"
However, if I had Egyptian Arabic set as main language of my operating system, the result should be like:
new Date(1542814586896).toLocaleDateString();
//-> "٢١‏/١١‏/٢٠١٨"
You may find more information about different locales & corresponding formats here.

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

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

How to get todays date in the same format as an existing date? [duplicate]

This question already has answers here:
How do I output an ISO 8601 formatted string in JavaScript?
(16 answers)
Closed 6 years ago.
I'm trying to get todays date in the same format as my variable. The variable outputs the below:
2016-05-18T23:00:00.000Z
Where as this code below outputs the below:
1463152286532
Date.now()
So the question is what do I have to do to Date.now() to make it the same format as my first blockquote.
The format you've shown at the top is the standard "ISO" format returned by Date#toISOString (spec | MDN). So:
new Date().toISOString();
Note: Date.now() returns a number, not a Date. It's the number of milliseconds since The Epoch (Jan 1st, 1970 at midnight UTC).

Categories