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 | Explanation |
---|---|
%a | Weekday: Sun, Mon |
%A | Weekday: Sunday, Monday |
%w | Weekday: 0,1,2… |
%d | Day of month: 01,02 |
%b | Months: Jan, Feb |
%B | Months: January, February |
%m | Months: 01,02 |
%y | Year without century: 11,12,13 |
%Y | Year with century: 2011,2012 |
%H | 24 Hours clock: from 00 to 23 |
%I | 12 Hours clock: from 01 to 12 |
%p | AM, PM |
%M | Minute: 00 to 59 |
%S | Second: 00 to 59 |
%f | Microseconds: 6 decimal numbers |
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))
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.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.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
.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.dateFormatEN
: Similarly, strftime
is applied with dateFormatEN
to format the datetime into the U.S. date style, which prioritizes the month before the day.2022-12-10 11:13:06.693783
10.12.2022 11:13:06
12/10/2022 11:13:06
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)
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 minuteprint(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.
2020-04-24 10:45:00