How to display Date Time in Different Formats in Python

Here I am going to discuss how to display date time in different formats using Python programming language. Python datetime module supplies classes for manipulating dates and times.

Date and time objects may be categorized as “aware” or “naive” depending on whether or not they include timezone information.

An aware object, such as time zone and daylight saving time information, can locate itself relative to other aware objects. An aware object represents a specific moment in time that is not open to interpretation.

A naive object does not contain enough information to unambiguously locate itself relative to other date/time objects. Whether a naive object represents Coordinated Universal Time (UTC), local time, or time in some other timezone is purely up to the program, just like it is up to the program whether a particular number represents metres, miles, or mass.

For applications requiring aware objects, datetime and time objects have an optional time zone information attribute, tzinfo, that can be set to an instance of a subclass of the abstract tzinfo class. These tzinfo objects capture information about the offset from UTC time, the time zone name, and whether daylight saving time is in effect.

Only one concrete tzinfo class, the timezone class, is supplied by the datetime module. The timezone class can represent simple timezones with fixed offsets from UTC, such as UTC itself or North American EST and EDT timezones.

In Python, dates are represented as objects. Therefore, when you manipulate them, you manipulate objects, not strings or timestamps.

The regular representation that is used by print() function using the str() function. It is most of the time the most common human readable format and is used to display easily in human readable format. So print(str(datetime.datetime(2020, 1, 6, 20, 53, 42))) gives you 2020-01-06 20:53:42. So you could see a nice date string.

The date, datetime, and time objects support a strftime(format) function, which is used to create a string representing the time under the control of an explicit format string. Here is a list of the format codes with their directive and meaning.

%a  Locale’s abbreviated weekday name. Ex: Sun, Mon, …, Sat (en_US);
		So, Mo, …, Sa (de_DE).
%A  Locale’s full weekday name. Ex: Sunday, Monday, …, Saturday (en_US);
 Sonntag, Montag, …, Samstag (de_DE).
%b  Locale’s abbreviated month name. Ex: Jan, Feb, …, Dec (en_US); Jan, Feb, …, Dez (de_DE).
%B  Locale’s full month name. Ex: January, February, …, December (en_US); Januar, Februar, …, Dezember (de_DE).
%c  Locale’s appropriate date and time representation. Ex: Tue Aug 16 21:30:00 1988 (en_US); Di 16 Aug 21:30:00 1988 (de_DE).   
%d  Day of the month as a decimal number [01,31]. Ex: 01, 02, …, 31.
%f  Microsecond as a decimal number [0,999999], zero-padded on the left. Ex: 000000, 000001, …, 999999.
%H  Hour (24-hour clock) as a decimal number [00,23]. Ex: 00, 01, …, 23.  
%I  Hour (12-hour clock) as a decimal number [01,12]. Ex: 01, 02, …, 12.  
%j  Day of the year as a decimal number [001,366]. Ex: 001, 002, …, 366. 
%m  Month as a decimal number [01,12]. Ex: 01, 02, …, 12.
%M  Minute as a decimal number [00,59]. Ex: 00, 01, …, 59.
%p  Locale’s equivalent of either AM or PM. Ex: AM, PM (en_US);	am, pm (de_DE).
%S  Second as a decimal number [00,61]. Ex: 00, 01, …, 59.
%U  Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0. Ex: 00, 01, …, 53.
%w  Weekday as a decimal number [0(Sunday),6]. Ex: 0, 1, …, 6.
%W  Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0. Ex: 00, 01, …, 53.
%x  Locale’s appropriate date representation. Ex: 08/16/88 (None); 08/16/1988 (en_US); 16.08.1988 (de_DE).
%X  Locale’s appropriate time representation. Ex: 21:30:00 (en_US); 21:30:00 (de_DE).
%y  Year without century as a decimal number [00,99]. Ex: 00, 01, …, 99.  
%Y  Year with century as a decimal number. Ex: 0001, 0002, …, 2013, 2014, …, 9998, 9999. 
%z  UTC offset in the form ±HHMM[SS[.ffffff]] (empty string if the object is naive). Ex: (empty), +0000, -0400, +1030, +063415, -030712.345216.
%Z  Time zone name (empty string if the object is naive). Ex: (empty), UTC, GMT.   
%%  A literal '%' character. Ex: %.
%G  ISO 8601 year with century representing the year that contains the greater part of the ISO week (%V). Ex: 0001, 0002, …, 2013, 2014, …, 9998, 9999.
%u  ISO 8601 weekday as a decimal number where 1 is Monday. Ex: 1, 2, …, 7.
%V  ISO 8601 week as a decimal number with Monday as the first day of the week. Week 01 is the week containing Jan 4. Ex: 01, 02, …, 53.

You can also combine the above directives to form a particular date and time value.

Prerequisites

Python 3.9.1

Date Time Representations

Here is the example of how to use the above directives to represent date or datetime for your different requirements.

import time
import datetime

print('Today: ', datetime.date.today())

print("Time in seconds since the epoch: %s" %time.time())
print("Current date and time: ", datetime.datetime.now())
print("Or Current date and time: ", datetime.datetime.now().strftime("%y-%m-%d-%H-%M"))

print("Full weekday name: ", datetime.date.today().strftime("%A"))
print("Abbreviated weekday name: ", datetime.date.today().strftime("%a"))

print("Full month name: ", datetime.date.today().strftime("%B"))
print("Abbreviated month name: ", datetime.date.today().strftime("%b"))

print("Appropriate date and time: ", datetime.date.today().strftime("%c"))

print("Day of the month as a decimal number: ", datetime.date.today().strftime("%d"))

print("Microsecond as a decimal number: ", datetime.date.today().strftime("%f"))

print("Hour (24-hour clock) as a decimal number: ", datetime.date.today().strftime("%H"))
print("Hour (12-hour clock) as a decimal number: ", datetime.date.today().strftime("%I"))

print("Day of the year as a decimal number: ", datetime.date.today().strftime("%j"))

print("Month as a decimal number: ", datetime.date.today().strftime("%m"))

print("Minute as a decimal number: ", datetime.date.today().strftime("%M"))

print("Either AM or PM: ", datetime.date.today().strftime("%p"))

print("Second as a decimal number: ", datetime.date.today().strftime("%S"))

print("Week number of the year: ", datetime.date.today().strftime("%U"))

print("Weekday as a decimal number: ", datetime.date.today().strftime("%w"))

print("Week number of the year: ", datetime.date.today().strftime("%W"))

print("Appropriate date representation: ", datetime.date.today().strftime("%x"))
print("Appropriate time representation: ", datetime.date.today().strftime("%X"))

print("Year without century as a decimal number: ", datetime.date.today().strftime("%y"))
print("Year with century as a decimal number: ", datetime.date.today().strftime("%Y"))

print("UTC offset in the form ±HHMM[SS[.ffffff]]: ", datetime.date.today().strftime("%z"))
print("Time zone name (empty string if the object is naive): ", datetime.date.today().strftime("%Z"))

print("A literal '%' character: ", datetime.date.today().strftime("%%"))

print("ISO 8601 year with century: ", datetime.date.today().strftime("%G"))

print("ISO 8601 weekday as a decimal number: ", datetime.date.today().strftime("%U"))

print("ISO 8601 week as a decimal number: ", datetime.date.today().strftime("%V"))

print("Combine directives to form date and time: ", datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S%z %p"))

Testing the Program

Running the above Python script will give you the following output:

date time in different formats in python

Hope you got an idea how to display datetime in different formats using Python program.

Source Code

Download

Leave a Comment