from datetime import date
dt=date.today().strftime('%Y-%m-%d')
print(dt)
Output
2019-09-17
Date with time
from datetime import datetime
dt=datetime.now().strftime('%c')
print(dt)
Output
Tue Sep 17 10:51:02 2019
import datetime
# Year, Month, date, Hour, Minute , Second, mircosecond
dt=datetime.datetime(2019,12,31,23,59,59,345234)
print(dt.strftime('%Y-%m-%d %H:%M:%S'))
Output
2019-12-31 23:59:59
Add Microsecond
print(dt.strftime('%Y-%m-%d %H:%M:%S - %f'))
output
2019-12-31 23:59:59 - 345234
dt=datetime.strptime(dt_column,'%Y-%m-%d').strftime('%d-%b-%Y') # sqlite
dt=datetime.strftime(dt_column,'%d-%b-%Y') # MySQL
List of format codes used with strftime()
%a | Weekday (short) : Sun, Mon ... |
%A | Weekday (long ) :Sunday, Monday ... |
%w | Weekday in decimal : 0,1 ... (Here Sun=0, Mon=1) ... |
%d | Day of the month with zero padded : 01,02,03 .... 11,12.... |
%b | Month ( short) : Jan, Feb ... |
%B | Month ( Long ) : January , February ... |
%m | Month ( number ) : 01,02 ... 11,12 |
%y | Year ( short ) : 01,02 ... 19,20 ... |
%Y | Year ( Long ) : 1998,2013 ... |
%H | Hour ( 24 hour ) : 01,02.. 22,23 |
%I | Hour (12 Hour ): 01,02 ..12 |
%p | AM or PM : or am mp ( de_DE). |
%M | Minutes : 01,02.. 59 |
%S | Seconds : 01,02 .. 59 |
%f | Microsecond : 000001,000002 .. 999999 |
%z | UTC offset in terms of HHMM ( + or - ) : 0000,-0130,0530.. |
%Z | Time zone name :UTC, CST , EST |
%j | day of the year ( zero padded ) : 004,121 .. 365 |
%U | Weekday of the Year ( zero padded), Sunday as first day of the week : 00,01,02,03 .. 53 |
%W | Weekday of the Year ( zero padded), Monday as first day of the week : 00,01,02,03 .. 53 |
%c | Locale’s appropriate date and time : Tue Dec 31 23:59:59 2019 |
%x | Locale’s appropriate date : 12/31/19 |
%X | Locale’s appropriate Time : 23:59:59 |
import time
h=time.strftime('%I') # getting local hour in 12 hours format as string
m=time.strftime('%M') # getting local minute as string
s=int(time.strftime('%S')) # getting local second value as integer using int()
h=int(h)*2 # to multiply the hour after changing to integer
print (h)
from datetime import date
dt=int(date.today().strftime('%U')) # integer output of string number
if(dt%2==0):
print('Yes')
else:
print('No')
All Date Objects strptime() string to Date and time object