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, microsecond
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')
from datetime import datetime
import locale
# Set locale to German (Germany)
locale.setlocale(locale.LC_TIME, 'de_DE.utf8')
now = datetime.now()
formatted_date = now.strftime('%A, %d. %B %Y')
print(formatted_date) # Output: 'Dienstag, 17. September 2019'
In this example, we set the locale to German and format the current date to display the day of the week, day, month, and year in German. Ensure that the desired locale is installed on your system; otherwise, you may encounter a locale.Error.
from datetime import datetime
import pytz
# Set timezone to US/Eastern
tz = pytz.timezone('US/Eastern')
now = datetime.now(tz)
formatted_date = now.strftime('%Y-%m-%d %H:%M:%S %Z%z')
print(formatted_date) # Output: '2019-09-17 10:51:02 EDT-0400'
In this example, we use the pytz library to set the timezone to US/Eastern and format the current date and time to include the time zone abbreviation (%Z) and the UTC offset (%z).
from datetime import datetime
now = datetime.now()
week_number = now.strftime('%U')
print('Week number:', week_number) # Output: 'Week number: 38'
In this example, %U
is used to get the week number of the year, with Sunday as the first day of the week. Alternatively, %W
can be used if Monday is considered the first day of the week.
When using strftime()
, it's important to be aware of certain pitfalls to ensure your code behaves as expected.
strftime()
can vary across platforms because Python calls the platform C library's strftime()
function. To see the full set of format codes supported on your platform, consult the strftime(3) documentation.%m
and %d
produce zero-padded results (e.g., 01
for January). If you prefer non-zero-padded results, you can remove the padding using platform-specific directives. For example, on Windows, you can use %-m
and %-d
to remove zero-padding. More details can be found on Enthought.%c
, %x
, %X
) depends on the current locale setting. Ensure that the appropriate locale is set in your environment to get the desired output.Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.