Change Timezone for Date object Python

2024/9/19 20:48:44

Hello I am using Pythonanywhere and when I call

from datetime import *print date.today().day

It is printing a different day than the day it is where I live (Austin, Texas). I figured it is because there is a timezone difference. How would I tell the date object where I live so it gets the right timezone. Thanks in advance

Answer

The most robust way to do this is to use pytz. You can install it simply with pip install pytz.

To get the local date with pytz, you can simply do this (note that the date.today method will not take a timezone):

>>> from datetime import datetime
>>> import pytz
>>> local_date = datetime.now(pytz.timezone('US/Central'))  # use datetime here
>>> local_date.date()                                       # now call date method
datetime.date(2014, 11, 30)

Compare that to the present Greenwich date:

>>> greenwich_date = datetime.now(pytz.timezone('Etc/Greenwich'))
>>> greenwich_date.date()
datetime.date(2014, 12, 1)
https://en.xdnf.cn/q/72384.html

Related Q&A

Multiprocessing Pool - how to cancel all running processes if one returns the desired result?

Given the following Python code: import multiprocessingdef unique(somelist):return len(set(somelist)) == len(somelist)if __name__ == __main__:somelist = [[1,2,3,4,5,6,7,8,9,10,11,12,13,2], [1,2,3,4,5],…

Pandas: Product of specific columns

Finding the product of all columns in a dataframe is easy:df[Product] = df.product(axis=1)How can I specify which column names (not column numbers) to include in the product operation?From the help pa…

instagram.bind.InstagramClientError: Unable to parse response, not valid JSON

Made myself a simple Instagram client to make authenticated requests to their API. However, running it keeps throwing the following errorTraceback (most recent call last):File "request-ig-data.py&…

Using jinja to send data to Javascript

I have Python code, in which Im using jinja to send data to a template in Flask. I can access the code just find in HTML, but when I try displaying the data in Javascript, it doesnt work. For example, …

celery: Substantial drift from

I have quite a problem with celery on my distribted system. I have couple of machines among different localizations and Ive got a lot of warnings in my log files like:"Substantial drift from celer…

jinja2 link to static files

I am trying to understand how to create a link to static files in jinja2.Everything I look up relates to Flask whereas I am using just webapp2 at this stage.My main.py file looks as follows:import os i…

How to upgrade tensorflow with GPU on google colaboratory

Currently google colaboratory uses tensorflow 1.4.1. I want to upgrade it to 1.5.0 version. Each time when i executed !pip install --upgrade tensorflow command, notebook instance succesfully upgrades t…

How to print warnings and errors when using setuptools (pip)

I am using setuptools to package code such that it can be easily installed using cd project_name && pip install .During the setup process, I want to warn the user about pre-existing config file…

TypeError: not supported between instances of State and State PYTHON 3

I am trying to utilize a PriorityQueue from the queue class. However, im having issues putting custom objects into my PQ. I have implemented the __cmp__ function below:def __cmp__(self, other):return (…

Threading and information passing -- how to

To reframe from confusion i have edited the question:one.pyimport threading count = 5 dev = threading.Thread(name=dev, target=dev,args=(workQueue,count,)) dev.setDaemon(True) dev.start() workQueue = Qu…