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 :: bash python csv to json 
Python :: the list of prime number in a given range python 
Python :: label encoding in python 
Python :: color python 
Python :: Python program to draw star 
Python :: pandas replace nan with mean 
Python :: pywebcopy 
Python :: check all values in dictionary python 
Python :: create dataframe from two variables 
Python :: python convert from float to decimal 
Python :: concat columns pandas dataframe 
Python :: train_test_split sklearn 
Python :: strip first occurence of substring python 
Python :: move items from one list to another python 
Python :: print pretty in python 
Python :: python sort list 
Python :: no module named pyinstaller 
Python :: Select rows without NaN in specific column 
Python :: import argv python 
Python :: effektivwert python 
Python :: check remote port is open or not using python 
Python :: how to update list in python 
Python :: python network programming 
Python :: python remove suffix 
Python :: get all subsets of a list python 
Python :: declare empty array of complex type python 
Python :: add time and date to datetime 
Python :: python except print error type 
Python :: importing database in dataframe using sqlalchemy 
Python :: clean column names pandas 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =