By default, the datetime
object is naive
in Python, so you need to make both of them either naive or aware datetime
objects. This can be done using:
import datetime import pytz utc=pytz.UTC challenge.datetime_start = utc.localize(challenge.datetime_start) challenge.datetime_end = utc.localize(challenge.datetime_end) # now both the datetime objects are aware, and you can compare them
Note: This would raise a ValueError
if tzinfo
is already set. If you are not sure about that, just use
start_time = challenge.datetime_start.replace(tzinfo=utc) end_time = challenge.datetime_end.replace(tzinfo=utc)
BTW, you could format a UNIX timestamp in datetime.datetime object with timezone info as following
d = datetime.datetime.utcfromtimestamp(int(unix_timestamp)) d_with_tz = datetime.datetime( year=d.year, month=d.month, day=d.day, hour=d.hour, minute=d.minute, second=d.second, tzinfo=pytz.UTC)
Related Posts:
- Python datetime to string without microsecond component
- Convenient way to deal with ValueError: cannot reindex from a duplicate axis
- Where does pip install its packages?
- Where does pip install its packages?
- django: TypeError: ‘tuple’ object is not callable
- Converting string into datetime
- Getting TypeError: __init__() missing 1 required positional argument: ‘on_delete’ when trying to add parent table after child table with entries
- Can’t fix “zipimport.ZipImportError: can’t decompress data; zlib not available” when I type in “python3.6 get-pip.py”
- No module named ‘psycopg2’
- Getting TypeError: __init__() missing 1 required positional argument: ‘on_delete’ when trying to add parent table after child table with entries
- No module named MySQLdb
- No module named MySQLdb
- How to check Django version
- Django: ImproperlyConfigured: The SECRET_KEY setting must not be empty
- is not JSON serializable
- Django: ImproperlyConfigured: The SECRET_KEY setting must not be empty
- Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock
- django:django.core.exceptions.AppRegistryNotReady: Apps aren’t loaded yet
- No module named pkg_resources
- Django upgrading to 1.9 error “AppRegistryNotReady: Apps aren’t loaded yet.”
- How do I do a case-insensitive string comparison?
- django import error – No module named core.management
- Forbidden (403) CSRF verification failed. Request aborted. Even using the {% csrf_token %}
- How to compare two dates?
- Can only use .dt accessor with datetimelike values
- Converting between datetime and Pandas Timestamp objects
- Django 2.1.3 Error: __init__() takes 1 positional argument but 2 were given
- Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock
- Setting Django up to use MySQL
- Check if two unordered lists are equal
- django:django.core.exceptions.AppRegistryNotReady: Apps aren’t loaded yet
- OSError – Errno 13 Permission denied
- Pylint “unresolved import” error in Visual Studio Code
- How do I do a case-insensitive string comparison?
- Django – no such table exception
- django MultiValueDictKeyError error, how do I deal with it
- Django – no such table exception
- Comparing two dictionaries and checking how many (key, value) pairs are equal
- Is there a list of Pytz Timezones?
- Django – “no module named django.core.management”
- How to convert string to datetime format in pandas python?
- Convert datetime to Unix timestamp and convert it back in python
- Convert date to datetime in Python
- How do I get the day of week given a date?
- Error: “dictionary update sequence element #0 has length 1; 2 is required” on Django 1.4
- NumPy array is not JSON serializable
- You are trying to add a non-nullable field ‘new_field’ to userprofile without a default
- pip install -r requirements.txt [Errno 2] No such file or directory: ‘requirements.txt’
- super(type, obj): obj must be an instance or subtype of type
- Parse date string and change format
- Django Rest Framework — no module named rest_framework
- Error: “dictionary update sequence element #0 has length 1; 2 is required” on Django 1.4
- Python Django: You’re using the staticfiles app without having set the STATIC_ROOT setting
- sqlite3.OperationalError: unable to open database file
- TypeError: int() argument must be a string, a bytes-like object or a number, not ‘list’
- Django reverse lookup of foreign keys
- ImportError: No module named ‘django.core.urlresolvers’
- datetime to string with series in pandas
- How to fix error “ERROR: Command errored out with exit status 1: python.” when trying to install django-heroku using pip
- How to set environment variables in PyCharm?
- How does tuple comparison work in Python?
- H14 error in heroku – “no web processes running”
- What does this Django regular expression mean? `?P`
- OperationalError: database is locked
- Is it better to use path() or url() in urls.py for django 2.0?
- Convert Pandas Column to DateTime
- What is a “slug” in Django?
- How do I find the time difference between two datetime objects in python?
- TemplateDoesNotExist at /
- No module named django but it is installed
- What is the easiest way to clear a database from the CLI with manage.py in Django?
- What does on_delete do on Django models?
- Check if an object exists
- How do I calculate the date six months from the current date using the datetime Python module?
- Find object in list that has attribute equal to some value (that meets any condition)
- How to check if a user is logged in (how to properly use user.is_authenticated)?
- The view didn’t return an HttpResponse object. It returned None instead
- CSV new-line character seen in unquoted field error
- gunicorn.errors.HaltServer:
django - coercing to Unicode: need string or buffer, NoneType found when rendering in django admin
- coercing to Unicode: need string or buffer, NoneType found when rendering in django admin
- Get current time in milliseconds in Python?
- How to query as GROUP BY in django?
- Error loading MySQLdb module: No module named ‘MySQLdb’
- What is related_name used for?
- TypeError: ManyRelatedManager object is not iterable
- What is the most efficient way to store a list in the Django models?
- Line is too long. Django PEP8
- How can I convert a datetime object to milliseconds since epoch (unix time) in Python?
- __init__() got an unexpected keyword argument ‘user’
- Extract file name from path, no matter what the os/path format
- Running Python from Atom
- TypeError: can’t use a string pattern on a bytes-like object in re.findall()
- Displaying better error message than “No JSON object could be decoded”
- ‘pip install’ fails for every package (“Could not find a version that satisfies the requirement”) [duplicate]
- Python multiprocessing.Pool: AttributeError
- Convert string to ASCII value python
- Python 3.6 import requests
- Tensorflow 2.0 – AttributeError: module ‘tensorflow’ has no attribute ‘Session’
- Can’t concat bytes to str