Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Reading Date and Time from String | Working with Times
Dealing with Dates and Times in Python
course content

Conteúdo do Curso

Dealing with Dates and Times in Python

Dealing with Dates and Times in Python

1. Working with Dates
2. Working with Times
3. Timezones and Daylight Savings Time (DST)
4. Working with Dates and Times in pandas

Reading Date and Time from String

By this time we can convert dates and times into strings of desired formats. But there is one important moment - into strings. The functionality we learned before can not be applied to strings. So, there has to be a revert way.

Of course, it exists! Consider datetime object named test. We used test.strftime(pattern) to convert datetime object into some pattern string. To convert string s into datetime object you need to use datetime.strptime(s, pattern). This will convert the string into a default datetime object. The pattern within the function - is the pattern of string you want to convert into datetime object. So, there you need to detect all the necessary codes satisfying the string. For example, imagine we have the string "1 November, 2021". There we can see the day of the month, month name, and 4-digit year. Therefore the necessary codes are %d, %B, and %Y.

123456789
# Load classes from library from datetime import datetime # String with date s = '1 November, 2021' # Parse string to datetime dt = datetime.strptime(s, "%d %B, %Y") print(f"String '{s}' converted to datetime: {dt}")
copy

Tarefa

You are given two strings d1 and d2 with dates and times. You need to convert them to datetime objects and save them in variables dt1 and dt2 respectively. Read the second string as May 23, 2019. Follow the table with the codes below, or take the hint to see the codes needed.

Tarefa

You are given two strings d1 and d2 with dates and times. You need to convert them to datetime objects and save them in variables dt1 and dt2 respectively. Read the second string as May 23, 2019. Follow the table with the codes below, or take the hint to see the codes needed.

Format codeMeaningExample
%dDay of the month as a zero-padded decimal number01, 02, ..., 30, 31
%mMonth as a zero-padded decimal number01, 02, ..., 11, 12
%yYear without century as a zero-padded decimal number00, 01, 02, ..., 98, 99
%YYear with century as a decimal number0001, 0002, 1999, 2000, 2001, ...
%bMonth as locale’s abbreviated nameJan, Feb, ..., Nov, Dec
%BMonth as locale’s full nameJanuary, February, ..., November, December
%aWeekday as locale’s abbreviated nameSun, Mon, ..., Fri, Sat
%AWeekday as locale’s full nameSunday, Monday, ..., Friday, Saturday
%HHour (24-hour clock) as a zero-padded decimal number00, 01, ..., 23, 24
%MMinute as a zero-padded decimal number00, 01, ..., 58, 59
%SSecond as a zero-padded decimal number00, 01, ..., 58, 59
%HHour (24-hour clock) as a zero-padded decimal number00, 01, ..., 22, 23
%IHour (12-hour clock) as a zero-padded decimal number01, 02, ..., 11, 12
%pLocale’s equivalent of either AM or PMAM, PM

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 2. Capítulo 8
toggle bottom row

Reading Date and Time from String

By this time we can convert dates and times into strings of desired formats. But there is one important moment - into strings. The functionality we learned before can not be applied to strings. So, there has to be a revert way.

Of course, it exists! Consider datetime object named test. We used test.strftime(pattern) to convert datetime object into some pattern string. To convert string s into datetime object you need to use datetime.strptime(s, pattern). This will convert the string into a default datetime object. The pattern within the function - is the pattern of string you want to convert into datetime object. So, there you need to detect all the necessary codes satisfying the string. For example, imagine we have the string "1 November, 2021". There we can see the day of the month, month name, and 4-digit year. Therefore the necessary codes are %d, %B, and %Y.

123456789
# Load classes from library from datetime import datetime # String with date s = '1 November, 2021' # Parse string to datetime dt = datetime.strptime(s, "%d %B, %Y") print(f"String '{s}' converted to datetime: {dt}")
copy

Tarefa

You are given two strings d1 and d2 with dates and times. You need to convert them to datetime objects and save them in variables dt1 and dt2 respectively. Read the second string as May 23, 2019. Follow the table with the codes below, or take the hint to see the codes needed.

Tarefa

You are given two strings d1 and d2 with dates and times. You need to convert them to datetime objects and save them in variables dt1 and dt2 respectively. Read the second string as May 23, 2019. Follow the table with the codes below, or take the hint to see the codes needed.

Format codeMeaningExample
%dDay of the month as a zero-padded decimal number01, 02, ..., 30, 31
%mMonth as a zero-padded decimal number01, 02, ..., 11, 12
%yYear without century as a zero-padded decimal number00, 01, 02, ..., 98, 99
%YYear with century as a decimal number0001, 0002, 1999, 2000, 2001, ...
%bMonth as locale’s abbreviated nameJan, Feb, ..., Nov, Dec
%BMonth as locale’s full nameJanuary, February, ..., November, December
%aWeekday as locale’s abbreviated nameSun, Mon, ..., Fri, Sat
%AWeekday as locale’s full nameSunday, Monday, ..., Friday, Saturday
%HHour (24-hour clock) as a zero-padded decimal number00, 01, ..., 23, 24
%MMinute as a zero-padded decimal number00, 01, ..., 58, 59
%SSecond as a zero-padded decimal number00, 01, ..., 58, 59
%HHour (24-hour clock) as a zero-padded decimal number00, 01, ..., 22, 23
%IHour (12-hour clock) as a zero-padded decimal number01, 02, ..., 11, 12
%pLocale’s equivalent of either AM or PMAM, PM

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 2. Capítulo 8
toggle bottom row

Reading Date and Time from String

By this time we can convert dates and times into strings of desired formats. But there is one important moment - into strings. The functionality we learned before can not be applied to strings. So, there has to be a revert way.

Of course, it exists! Consider datetime object named test. We used test.strftime(pattern) to convert datetime object into some pattern string. To convert string s into datetime object you need to use datetime.strptime(s, pattern). This will convert the string into a default datetime object. The pattern within the function - is the pattern of string you want to convert into datetime object. So, there you need to detect all the necessary codes satisfying the string. For example, imagine we have the string "1 November, 2021". There we can see the day of the month, month name, and 4-digit year. Therefore the necessary codes are %d, %B, and %Y.

123456789
# Load classes from library from datetime import datetime # String with date s = '1 November, 2021' # Parse string to datetime dt = datetime.strptime(s, "%d %B, %Y") print(f"String '{s}' converted to datetime: {dt}")
copy

Tarefa

You are given two strings d1 and d2 with dates and times. You need to convert them to datetime objects and save them in variables dt1 and dt2 respectively. Read the second string as May 23, 2019. Follow the table with the codes below, or take the hint to see the codes needed.

Tarefa

You are given two strings d1 and d2 with dates and times. You need to convert them to datetime objects and save them in variables dt1 and dt2 respectively. Read the second string as May 23, 2019. Follow the table with the codes below, or take the hint to see the codes needed.

Format codeMeaningExample
%dDay of the month as a zero-padded decimal number01, 02, ..., 30, 31
%mMonth as a zero-padded decimal number01, 02, ..., 11, 12
%yYear without century as a zero-padded decimal number00, 01, 02, ..., 98, 99
%YYear with century as a decimal number0001, 0002, 1999, 2000, 2001, ...
%bMonth as locale’s abbreviated nameJan, Feb, ..., Nov, Dec
%BMonth as locale’s full nameJanuary, February, ..., November, December
%aWeekday as locale’s abbreviated nameSun, Mon, ..., Fri, Sat
%AWeekday as locale’s full nameSunday, Monday, ..., Friday, Saturday
%HHour (24-hour clock) as a zero-padded decimal number00, 01, ..., 23, 24
%MMinute as a zero-padded decimal number00, 01, ..., 58, 59
%SSecond as a zero-padded decimal number00, 01, ..., 58, 59
%HHour (24-hour clock) as a zero-padded decimal number00, 01, ..., 22, 23
%IHour (12-hour clock) as a zero-padded decimal number01, 02, ..., 11, 12
%pLocale’s equivalent of either AM or PMAM, PM

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

By this time we can convert dates and times into strings of desired formats. But there is one important moment - into strings. The functionality we learned before can not be applied to strings. So, there has to be a revert way.

Of course, it exists! Consider datetime object named test. We used test.strftime(pattern) to convert datetime object into some pattern string. To convert string s into datetime object you need to use datetime.strptime(s, pattern). This will convert the string into a default datetime object. The pattern within the function - is the pattern of string you want to convert into datetime object. So, there you need to detect all the necessary codes satisfying the string. For example, imagine we have the string "1 November, 2021". There we can see the day of the month, month name, and 4-digit year. Therefore the necessary codes are %d, %B, and %Y.

123456789
# Load classes from library from datetime import datetime # String with date s = '1 November, 2021' # Parse string to datetime dt = datetime.strptime(s, "%d %B, %Y") print(f"String '{s}' converted to datetime: {dt}")
copy

Tarefa

You are given two strings d1 and d2 with dates and times. You need to convert them to datetime objects and save them in variables dt1 and dt2 respectively. Read the second string as May 23, 2019. Follow the table with the codes below, or take the hint to see the codes needed.

Format codeMeaningExample
%dDay of the month as a zero-padded decimal number01, 02, ..., 30, 31
%mMonth as a zero-padded decimal number01, 02, ..., 11, 12
%yYear without century as a zero-padded decimal number00, 01, 02, ..., 98, 99
%YYear with century as a decimal number0001, 0002, 1999, 2000, 2001, ...
%bMonth as locale’s abbreviated nameJan, Feb, ..., Nov, Dec
%BMonth as locale’s full nameJanuary, February, ..., November, December
%aWeekday as locale’s abbreviated nameSun, Mon, ..., Fri, Sat
%AWeekday as locale’s full nameSunday, Monday, ..., Friday, Saturday
%HHour (24-hour clock) as a zero-padded decimal number00, 01, ..., 23, 24
%MMinute as a zero-padded decimal number00, 01, ..., 58, 59
%SSecond as a zero-padded decimal number00, 01, ..., 58, 59
%HHour (24-hour clock) as a zero-padded decimal number00, 01, ..., 22, 23
%IHour (12-hour clock) as a zero-padded decimal number01, 02, ..., 11, 12
%pLocale’s equivalent of either AM or PMAM, PM

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 2. Capítulo 8
Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt