Python Module: Datetime

Datetime

Programs often include dates and time to perform interactive greetings, calculate age, stamping backups, etc. Here is how to use this class.

import datetime
now = datetime.datetime.now()

# show the day and month using strftime() function
print(now.strftime(%A%m))

For those who are familiar with Linux datetime directives, these symbols will seem very familiar:

Directive Description Example
% Init command character. When precedes itself, becomes escape character. %% => %
%a Abbreviated weekday Wed
%A Weekday Wednesday
%b Abbreviated month name Jan
%B Month name January
%c Copy local date and time Fri Apr 19 13:33:17 2019
%d Day of month 01-31 30
%f Ticks value up to 999999 548513
%H Hour 00-23 (00 is 23 is 2400 hours) 05
%I Hour 00-12 (up to 1300 hours) 05
%j Day number of year 001-366 365
%k hour, 24-hour, leading space as needed, 0-23 _5
%l hour, 12-hour format 1-12 12
%m Month as a number 01-12 12
%M Minute 00-59 41
%p AM/PM PM
%S Second 00-59 08
%U Week number of year, Sunday as the first day of week, 00-53 52
%w Weekday as a number 0-6, 0 is Sunday 3
%W Week number of year, Monday as the first day of week, 00-53 52
%x Local version of date 12/31/18
%X Local version of time 17:41:00
%y Year, short version, without century 18
%Y Year, full version 2018
%z UTC offset +0100
%Z Timezone CST

Leave a Reply

Your email address will not be published. Required fields are marked *