Python vs Javascript DateTime

I am trying to convert a Javascript API call to Python. The javascript working code works fine by creating a timestamp like this:

var curdate = new Date(); var gmtstring = curdate.toGMTString(); var utc = Date.parse(gmtstring) / 1000; 

This result (this is the number of seconds since the era) is subsequently hashed and used in the API call, but this is the corresponding section. If anyone can tell me the correct way to convert this, we will be very grateful.

Here are some details about the different results of different methods:

Javascript (valid API result)

 var curdate = new Date(2013, 7, 10); var gmtstring = curdate.toGMTString(); var utc = Date.parse(gmtstring) / 1000; 

Result: 1376089200

Python (invalid API result)

 from datetime import datetime import calendar d = datetime(2013, 8, 10) calendar.timegm(d.utctimetuple()) 

Result: 1376092800

I obviously missed something, can someone enlighten me on this?

Update

I initially made a mistake in my examples, since Javascript uses dates based on 0 and Python dates use 1.

Jonathon kindly explained that the difference in values ​​is different because Python is set to UTC by default when Javascript uses the local time zone by default. In my case, this is GMT, which is required by the API. Now I just need to get this result in Python.

Answer

The solution was a mismatch of the time zones provided. Although I'm still having problems with a third-party api, I am at least getting the right times now.

Perhaps this can be cleared:

 from datetime import datetime import calendar import time import pytz def GenerateTimeStamp(d): europe = pytz.timezone('Europe/London') d = europe.localize(d) tuple = d.utctimetuple() timestamp = int(time.mktime(tuple)) return timestamp 

Just specify the date and time:

 GenerateTimeStamp(datetime(2013, 8, 10)) 

or

 GenerateTimeStamp(datetime.utcnow()) 

As a side note, if you are trying to do this and want to install pytz 1 using pip, you can get it using pre tag 2 :

 pip install --pre pytz 
+6
source share
3 answers

For JavaScript dates, the month argument is 0 - 11 . So, in August you will want to pass 7 .

An integer value representing the month, starting from 0 for January to December 11th.

They also have different default time zones, with Python doing UTC by default and defaulting to custom "local" time zones in JavaScript.

You can use Date.UTC() , which returns the timestamp for the equivalent in JavaScript.

 var utc = Date.UTC(2013, 7, 10) / 1000; // 1376092800 

Note: you can use getTime() to get the Date timestamp.

 var curdate = new Date(2013, 7, 10); var utc = curdate.getTime() / 1000; 
+6
source

new Date(2013, 8, 10) in javascript means September / 10/2013.

datetime(2013, 8, 10) in Python means August / 10/2013.

You are comparing two different dates.

Local time vs UTC

Results may vary (time zone for my system: Asia / Seoul).

Javascript

 > new Date(2013, 7, 10) / 1000 1376060400 > Date.UTC(2013, 7, 10) / 1000 1376092800 

Python

 >>> import calendar >>> import datetime >>> import time >>> time.mktime(datetime.datetime(2013, 8, 10).timetuple()) 1376060400.0 >>> calendar.timegm(datetime.datetime(2013, 8, 10).timetuple()) 1376092800 

See Python - calendar.timegm () vs. time.mktime ()

+3
source

In addition to Jonathan's answer above, I could recommend this on the python side:

  d = datetime(2013, 8, 10) d.strftime("%s") 

This will return: '1376107200'

+1
source

Source: https://habr.com/ru/post/951452/


All Articles