strftime()

strftime() : Returns the string by using format by taking date object as input .

Today's date details using strftime()

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

Using date object

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

From Database to display as string

We can collect date column value from MySQL or SQLite database and display in any format using the table below. Here dt_column is the date value from table in YYYY-MM-DD format.
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()
%aWeekday (short) : Sun, Mon ...
%AWeekday (long ) :Sunday, Monday ...
%wWeekday in decimal : 0,1 ... (Here Sun=0, Mon=1) ...
%dDay of the month with zero padded : 01,02,03 .... 11,12....
%bMonth ( short) : Jan, Feb ...
%BMonth ( Long ) : January , February ...
%mMonth ( number ) : 01,02 ... 11,12
%yYear ( short ) : 01,02 ... 19,20 ...
%YYear ( Long ) : 1998,2013 ...
%HHour ( 24 hour ) : 01,02.. 22,23
%IHour (12 Hour ): 01,02 ..12
%pAM or PM : or am mp ( de_DE).
%MMinutes : 01,02.. 59
%SSeconds : 01,02 .. 59
%fMicrosecond : 000001,000002 .. 999999
%zUTC offset in terms of HHMM ( + or - ) : 0000,-0130,0530..
%ZTime zone name :UTC, CST , EST
%jday of the year ( zero padded ) : 004,121 .. 365
%UWeekday of the Year ( zero padded), Sunday as first day of the week : 00,01,02,03 .. 53
%WWeekday of the Year ( zero padded), Monday as first day of the week : 00,01,02,03 .. 53
%cLocale’s appropriate date and time : Tue Dec 31 23:59:59 2019
%xLocale’s appropriate date : 12/31/19
%XLocale’s appropriate Time : 23:59:59

Using Time

We can get hour, minute and second values and then update to integer by using int() . Using these values we can perform calculations. Use the above formats to get Minute and second values.
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)

Week Number %U

Based on Week number print Yes ( if even ) or print No ( if odd )
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
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer