• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: How to decode js timestamp to python timestamp and vice versa?

Resolved: How to decode js timestamp to python timestamp and vice versa?

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I am making a request to one of server and getting response like this:
I am not able to understand the time format it is using and if I have to make a post request to that server with the current time, I am unable to convert the python timestamp into that format.
In python, if I convert the current time into a timestamp, it looks like this:
This gives me results in something like:
But in server response, there are already a few zeros at the end even though the time is live from there, what is the format and how to convert python time into that format?

Answer:

We know that datetime.now().strftime("%s%f") is a timestamps measured in microseconds (since %s is seconds and %f is microseconds).
And 1655375216687000 and 1655375242179881 are the same length, which suggests your source data is also a timestamp measured in microseconds. (The fact that the last three digits are always 000 suggests that it was originally derived from a source with milliseconds precision, but the value is clearly microseconds).
We can use datetime.fromtimestamp: https://docs.python.org/3/library/datetime.html#datetime.datetime.fromtimestamp
However this expects a time in seconds, “as returned by time.time()“
So we can:
Which returns datetime.datetime(2022, 6, 16, 11, 26, 56, 687000)

If you have better answer, please add a comment about this, thank you!

javascript python time
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How can I copy files using the ansible.builtin.copy module and avoid conflicting file names?

26/03/2023

Resolved: Reshape tensors of unknown shape with tf.function

26/03/2023

Resolved: Use Svelte Component as Slot

26/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.