Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to get the local time in python

from datetime import datetime
import time
now = datetime.now()
while True:
    print(now.strftime("local time :- %X"))
    time.sleep(2)
Comment

local time in 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 :: How to Get the length of all items in a list of lists in Python 
Python :: .replace python 
Python :: django strptime 
Python :: django delete instance 
Python :: pygame rect 
Python :: Python Pandas - How to write in a specific column in an Excel Sheet 
Python :: Reverse an string Using Loop in Python 
Python :: pandas dataframe check for values more then a number 
Python :: python print set 
Python :: python read hex file 
Python :: use argparse to call function and use argument in function 
Python :: librosa python 
Python :: variable globale python 
Python :: how to form .cleaned data in class based views in django 
Python :: python autoclick website 
Python :: how to print text in python 
Python :: pyttsx3 saving the word to speak 
Python :: python dict items 
Python :: Python operator to use for set union 
Python :: smooth interpolation python 
Python :: panda lambda function returns dataframe 
Python :: next power of 2 python 
Python :: python change all values in nested list 
Python :: open file in python network url 
Python :: python check empty string 
Python :: are logN and (lognN) same 
Python :: sorted multiple keys python 
Python :: Math Module cos() Function in python 
Python :: how to do more than or less than as a condition in pythonb 
Python :: cbind arrays python 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =