2

I've a JavaScript date string which I want to convert in understandable form. E.g.

new Date(1415988000000)

which gives the output

Fri Nov 14 2014 23:30:00 GMT+0530 (IST)

I want to do this in python. I don't want to use PyV8. Is there any other alternative ?

4
  • 1
    I'm just curious... why do you have to use JavaScript in Python? Are you writing some tool to analyze js code or what? Commented Nov 12, 2014 at 10:54
  • 3
    Since you're in a Python environment, what's wrong with using from datetime import date then foo = date(1415988000000) and for String representation use e.g. foo.ctime() Commented Nov 12, 2014 at 10:59
  • I'm getting this information (date) from the response (JSON format) of an api call which I need to convert in actual date format before storing in the database. Commented Nov 12, 2014 at 11:00
  • Sorry I meant foo = date.fromtimestamp(1415988000000 // 1000) (integer division) Commented Nov 12, 2014 at 11:06

1 Answer 1

3

You don't need to write Javascript in Python to convert the timestamps.

The following works in pure python.

Note:

  • The JS Date format is the number of milliseconds since Jan 1,1970 UTC.
  • Oddly enough, the python format for time.time() is the number of seconds since Jan 1, 1970 UTC.

This standard is sometimes called Unix time

First, for the milliseconds to seconds time conversion, you need to divide 1415988000000 by 1000, obtaining 1415988000

Then you can use the datetime library like this:

import datetime
d = datetime.datetime.fromtimestamp(1415988000)
print d

Obtaining:

2014-11-14 13:00:00

This print seems to have converted d to my TZ which is UTC-5.

So UTC time would be 18:00.

This explains the later time, 23:30, you receive in JS for the same stamp in IST or UTC+5:30

Sign up to request clarification or add additional context in comments.

2 Comments

Thank You ! Got it now. I didn't knew about the standard unix time that it is in seconds instead of milliseconds, so I was getting year out of range ValueError.
Yeah that will do it. Good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.