Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

current date to midnight

>>> import pytz
>>> from datetime import datetime, date, time
# To get the UTC time of current day (today) for a given time zone
# use the localize() function of the pytz. 
# The reason for this is that the localize() takes into account the Day Time Saving
# which niether the datetime constructors nor replace() account for.
# 
# To for example, the utc time of today in australia time zone:

import pytz
from datetime import datetime, date, time

# set time zone
tz = pytz.timezone("Australia/Melbourne")

# Get today's date in the current time zone (i.e local time)
todaydate = date.today() # you can also use date(2022, 2, 8)

# Get midnight of today (still local time)
# note time() without arguments will result to midnight

midnight_local = datetime.combine(todaydate, time())

print midnight_local

# convert this midnight datetime to the midnight datetime
# in the given time zone. In this case autralia time zone
austra_midnight = tz.localize(midnight_local)

print austra_midnight

# convert the midnight time in australia time zone to UTC
midnight_austra_utc = austra_midnight.astimezone(pytz.uct)

print midnight_austra_utc



# Ref:
# https://stackoverflow.com/questions/373370/how-do-i-get-the-utc-time-of-midnight-for-a-given-timezone
Comment

PREVIOUS NEXT
Code Example
Python :: get midnight of current day python 
Python :: subplots 
Python :: python sort list opposite 
Python :: python class without init 
Python :: database with python connection 
Python :: xlabel not showing matplotlib 
Python :: Python difference between filter and map 
Python :: how to create a subset of two columns in a dataframe 
Python :: django venv activate 
Python :: get first digit of number 
Python :: matplot image axis 
Python :: crypto trading bot python 
Python :: python inline if 
Python :: change column order pandas 
Python :: print specific key in dictionary python 
Python :: str count python 
Python :: pandas filter rows by column value regex 
Python :: Python NumPy append Function Example Appending arrays 
Python :: python MAX_INT 
Python :: tuple and for loop 
Python :: python string contains substring ignore case 
Python :: plot scattered dataframe 
Python :: check if value in dictionary keys python dataframe 
Python :: install pyimagesearch python3 
Python :: lower method in python 
Python :: iteration 
Python :: pytest fixture 
Python :: copy python 
Python :: python print in the same line 
Python :: django edit object foreign key id 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =