Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

get midnight of current day python

>>> 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 :: django custom user model 
Python :: pandas switch column levels 
Python :: odoo model 
Python :: tuple to string python 
Python :: python read xlsx file 
Python :: flask multuple parameters 
Python :: convert rgb image to binary in pillow 
Python :: 2nd to last index python 
Python :: remove emoji 
Python :: correlation with target variable python 
Python :: for i in array in range python 
Python :: discord.py create button 
Python :: python hash timestamp 
Python :: pandas find fifth caracter in field and change cell based on that number 
Python :: python numpy array subtract 
Python :: pyqt matplotlib 
Python :: Use len with list 
Python :: swapping upper case and lower case string python 
Python :: python string first letter uppercase and second letter in lowercase 
Python :: convert hex rgb to matplotlib color 
Python :: py quick sort 
Python :: print string in reverse order uing for loop python 
Python :: Subtract different times in Python 
Python :: iterate array python with index 
Python :: python global variable unboundlocalerror 
Python :: loops in python 
Python :: creating a dictionary 
Python :: binary search in python 
Python :: determine how 2 string si equal py 
Python :: extend python 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =