Working with strings

Python provides two useful functions to work with time strings with strftime() and strptime() from the datetime standard library.

With the strftime() function (string from time) you can create simple strings from date, datetime and time objects. It only needs a so-called format string, which describes what should be contained in the time string.

The strptime() functino in python is used to convert string to datetime object.

Code Formatting

CodeExplanation
%aWeekday: Sun, Mon
%AWeekday: Sunday, Monday
%wWeekday: 0,1,2…
%dDay of month: 01,02
%bMonths: Jan, Feb
%BMonths: January, February
%mMonths: 01,02
%yYear without century: 11,12,13
%YYear with century: 2011,2012
%H24 Hours clock: from 00 to 23
%I12 Hours clock: from 01 to 12
%pAM, PM
%MMinute: 00 to 59
%SSecond: 00 to 59
%fMicroseconds: 6 decimal numbers

Example 1: strftime() – date to string

import datetime

dateFormatDE = "%d.%m.%Y %H:%M:%S"
dateFormatEN = "%m/%d/%Y %H:%M:%S"
dt = datetime.datetime.now()

print(dt)
print(dt.strftime(dateFormatDE))
print(dt.strftime(dateFormatEN))
Code Explanation
Setting Date Formats
  • dateFormatDE: Defined to match the common date and time format used in Germany, it uses %d.%m.%Y %H:%M:%S to format dates in the day-month-year sequence followed by the time in hours, minutes, and seconds.
  • dateFormatEN: Set up for the typical U.S. date and time format, it uses %m/%d/%Y %H:%M:%S where the month comes before the day, both followed by the year and then the time.
Creating a datetime Object
  • dt: This variable is assigned the current date and time using datetime.datetime.now(), which fetches the local date and time at the moment of execution.
Printing Date and Time in Various Formats
  • Default Print of dt: The first print statement displays dt using the default string representation provided by the datetime object, which is typically in the format YYYY-MM-DD HH:MM:SS.ssssss.
  • Formatted Print Using dateFormatDE: The strftime method is used with dateFormatDE to convert the datetime object into a string formatted according to the German standard of day-month-year, making it more readable for audiences used to this format.
  • Formatted Print Using dateFormatEN: Similarly, strftime is applied with dateFormatEN to format the datetime into the U.S. date style, which prioritizes the month before the day.
Output
2022-12-10 11:13:06.693783
10.12.2022 11:13:06
12/10/2022 11:13:06

Example 2: strptime() – string to date

Here we convert a date in string format to a datetime object.

import datetime

dateString = "04/24/2020 10:45"
dt = datetime.datetime.strptime(dateString, "%m/%d/%Y %H:%M")

print(dt)
Code Explanation
Parsing a Date String
  • dateString: This variable holds a string that represents a specific date and time, formatted as “04/24/2020 10:45”. The format here uses the month/day/year hour:minute arrangement, which is commonly used in the United States.
  • dt: This variable is created by parsing dateString using the datetime.datetime.strptime method. The strptime method is designed to convert a string formatted date into a datetime object. The format string "%m/%d/%Y %H:%M" is passed along with the dateString to this method, which tells Python how to interpret and parse the different parts of the date string into a proper datetime object. Each part of the date and time in dateString matches up with the corresponding format codes:
    • %m for month
    • %d for day
    • %Y for a four-digit year
    • %H for hour in 24-hour format
    • %M for minute
Printing the Datetime Object
  • The print(dt) statement outputs the datetime object dt, showing the date and time in Python’s default datetime format, which typically looks like “YYYY-MM-DD HH:MM:SS”. In this case, since the time component in dateString does not include seconds, the seconds will be displayed as 00.

This approach is often used in applications where date and time data are initially available in string format. Such as data read from files or user inputs. This string need to be converted into datetime objects for further processing, manipulation, or computation.

Output
2020-04-24 10:45:00