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

Contenido del 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

Datetime Attributes

Like for date objects, there are also attributes for datetime allowing us to extract any part of the date and time we want. These are:

  • datetime.year - extracts year;
  • datetime.month - extracts month;
  • datetime.day - extracts day of the month;
  • datetime.hour - extracts hour from time;
  • datetime.minute - extracts minute from time;
  • datetime.second - extracts second from time;
  • datetime.microsecond - extracts microseconds from time.

For example, from the datetime object, we can extract only day, minute, and second.

123456789
# Load class from library from datetime import datetime # Create datetime object dt = datetime(2020, 11, 1, 11, 20, 25) # Extract day, minute and second print("Day of month:", dt.day) print(f"Minute:Second - {dt.minute}:{dt.second}")
copy

Tarea

Given datetime object dt with the assigned date "May 15, 2013, 18:10:52". Replace the first two ___ parts with the day and month of dt (like 15.5), and the last two ones with the hour and minute (like 18:10).

Tarea

Given datetime object dt with the assigned date "May 15, 2013, 18:10:52". Replace the first two ___ parts with the day and month of dt (like 15.5), and the last two ones with the hour and minute (like 18:10).

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 2. Capítulo 3
toggle bottom row

Datetime Attributes

Like for date objects, there are also attributes for datetime allowing us to extract any part of the date and time we want. These are:

  • datetime.year - extracts year;
  • datetime.month - extracts month;
  • datetime.day - extracts day of the month;
  • datetime.hour - extracts hour from time;
  • datetime.minute - extracts minute from time;
  • datetime.second - extracts second from time;
  • datetime.microsecond - extracts microseconds from time.

For example, from the datetime object, we can extract only day, minute, and second.

123456789
# Load class from library from datetime import datetime # Create datetime object dt = datetime(2020, 11, 1, 11, 20, 25) # Extract day, minute and second print("Day of month:", dt.day) print(f"Minute:Second - {dt.minute}:{dt.second}")
copy

Tarea

Given datetime object dt with the assigned date "May 15, 2013, 18:10:52". Replace the first two ___ parts with the day and month of dt (like 15.5), and the last two ones with the hour and minute (like 18:10).

Tarea

Given datetime object dt with the assigned date "May 15, 2013, 18:10:52". Replace the first two ___ parts with the day and month of dt (like 15.5), and the last two ones with the hour and minute (like 18:10).

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 2. Capítulo 3
toggle bottom row

Datetime Attributes

Like for date objects, there are also attributes for datetime allowing us to extract any part of the date and time we want. These are:

  • datetime.year - extracts year;
  • datetime.month - extracts month;
  • datetime.day - extracts day of the month;
  • datetime.hour - extracts hour from time;
  • datetime.minute - extracts minute from time;
  • datetime.second - extracts second from time;
  • datetime.microsecond - extracts microseconds from time.

For example, from the datetime object, we can extract only day, minute, and second.

123456789
# Load class from library from datetime import datetime # Create datetime object dt = datetime(2020, 11, 1, 11, 20, 25) # Extract day, minute and second print("Day of month:", dt.day) print(f"Minute:Second - {dt.minute}:{dt.second}")
copy

Tarea

Given datetime object dt with the assigned date "May 15, 2013, 18:10:52". Replace the first two ___ parts with the day and month of dt (like 15.5), and the last two ones with the hour and minute (like 18:10).

Tarea

Given datetime object dt with the assigned date "May 15, 2013, 18:10:52". Replace the first two ___ parts with the day and month of dt (like 15.5), and the last two ones with the hour and minute (like 18:10).

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Like for date objects, there are also attributes for datetime allowing us to extract any part of the date and time we want. These are:

  • datetime.year - extracts year;
  • datetime.month - extracts month;
  • datetime.day - extracts day of the month;
  • datetime.hour - extracts hour from time;
  • datetime.minute - extracts minute from time;
  • datetime.second - extracts second from time;
  • datetime.microsecond - extracts microseconds from time.

For example, from the datetime object, we can extract only day, minute, and second.

123456789
# Load class from library from datetime import datetime # Create datetime object dt = datetime(2020, 11, 1, 11, 20, 25) # Extract day, minute and second print("Day of month:", dt.day) print(f"Minute:Second - {dt.minute}:{dt.second}")
copy

Tarea

Given datetime object dt with the assigned date "May 15, 2013, 18:10:52". Replace the first two ___ parts with the day and month of dt (like 15.5), and the last two ones with the hour and minute (like 18:10).

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 2. Capítulo 3
Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt