Here are some examples using datatime module to get current time and date
# %% Import necessary library
from datetime import date , time , timezone , datetime
# %% Run using function .now to get right present time
datetime.now()
... datetime.datetime(2020, 8, 31, 11, 23, 42, 962037)
# %%
dt = datetime.now()
dt.strftime('%c')
... 'Mon Aug 31 10:45:55 2020'
# %% To get only time
t = dt.time()
t.strftime('%r')
... '10:45:55 AM'
# %% To get only date
d = date.today()
d.isoformat()
... '2020-08-31'
# %% You can also use this for various style
a.strftime('%m-%d') # month and date
... '08-31'
a.strftime('%m-%a') # month and day
'08-Mon'
Comments
Post a Comment