Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
What Time is it in ... ? | Timezones and Daylight Savings Time (DST)
Dealing with Dates and Times in Python
course content

Course Content

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

What Time is it in ... ?

As you might notice, the previous method simply labels to datetime timezone. The second approach is to convert existing datetime objects with no timezone to a certain timezone.

Let's consider the same datetime object as in the previous chapter, but this time with the second approach. The algorithm is the next:

  1. Create datetime object with no timezone;
  2. Create timezone object with a certain timezone name (optional);
  3. Apply .astimezone() method to datetime object with timezone object as an argument.
12345678910111213
# Load libraries and classes import pytz from pytz import timezone from datetime import datetime tz = timezone('Europe/Warsaw') # save timezone dt = datetime(2021, 11, 1, 11, 25, 0) # create datetime object dt_tz = dt.astimezone(tz) # convert dt to tz timezone # Testing print(f"Saved time: {dt}") print(f"Converted time: {dt_tz}")
copy

There you can see a clear difference: not only timezone was saved, but time was adjusted to timezone. Let's try to complete the task! Please note, that code is compiling on a dedicated server with GMT.

Task

Use the second approach to convert datetime object with a current timestamp to tz timezone:

  1. Set 'America/New_York' timezone in tz.
  2. Apply method to datetime object now to convert it to tz timezone.

Task

Use the second approach to convert datetime object with a current timestamp to tz timezone:

  1. Set 'America/New_York' timezone in tz.
  2. Apply method to datetime object now to convert it to tz timezone.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Section 3. Chapter 4
toggle bottom row

What Time is it in ... ?

As you might notice, the previous method simply labels to datetime timezone. The second approach is to convert existing datetime objects with no timezone to a certain timezone.

Let's consider the same datetime object as in the previous chapter, but this time with the second approach. The algorithm is the next:

  1. Create datetime object with no timezone;
  2. Create timezone object with a certain timezone name (optional);
  3. Apply .astimezone() method to datetime object with timezone object as an argument.
12345678910111213
# Load libraries and classes import pytz from pytz import timezone from datetime import datetime tz = timezone('Europe/Warsaw') # save timezone dt = datetime(2021, 11, 1, 11, 25, 0) # create datetime object dt_tz = dt.astimezone(tz) # convert dt to tz timezone # Testing print(f"Saved time: {dt}") print(f"Converted time: {dt_tz}")
copy

There you can see a clear difference: not only timezone was saved, but time was adjusted to timezone. Let's try to complete the task! Please note, that code is compiling on a dedicated server with GMT.

Task

Use the second approach to convert datetime object with a current timestamp to tz timezone:

  1. Set 'America/New_York' timezone in tz.
  2. Apply method to datetime object now to convert it to tz timezone.

Task

Use the second approach to convert datetime object with a current timestamp to tz timezone:

  1. Set 'America/New_York' timezone in tz.
  2. Apply method to datetime object now to convert it to tz timezone.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Section 3. Chapter 4
toggle bottom row

What Time is it in ... ?

As you might notice, the previous method simply labels to datetime timezone. The second approach is to convert existing datetime objects with no timezone to a certain timezone.

Let's consider the same datetime object as in the previous chapter, but this time with the second approach. The algorithm is the next:

  1. Create datetime object with no timezone;
  2. Create timezone object with a certain timezone name (optional);
  3. Apply .astimezone() method to datetime object with timezone object as an argument.
12345678910111213
# Load libraries and classes import pytz from pytz import timezone from datetime import datetime tz = timezone('Europe/Warsaw') # save timezone dt = datetime(2021, 11, 1, 11, 25, 0) # create datetime object dt_tz = dt.astimezone(tz) # convert dt to tz timezone # Testing print(f"Saved time: {dt}") print(f"Converted time: {dt_tz}")
copy

There you can see a clear difference: not only timezone was saved, but time was adjusted to timezone. Let's try to complete the task! Please note, that code is compiling on a dedicated server with GMT.

Task

Use the second approach to convert datetime object with a current timestamp to tz timezone:

  1. Set 'America/New_York' timezone in tz.
  2. Apply method to datetime object now to convert it to tz timezone.

Task

Use the second approach to convert datetime object with a current timestamp to tz timezone:

  1. Set 'America/New_York' timezone in tz.
  2. Apply method to datetime object now to convert it to tz timezone.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

As you might notice, the previous method simply labels to datetime timezone. The second approach is to convert existing datetime objects with no timezone to a certain timezone.

Let's consider the same datetime object as in the previous chapter, but this time with the second approach. The algorithm is the next:

  1. Create datetime object with no timezone;
  2. Create timezone object with a certain timezone name (optional);
  3. Apply .astimezone() method to datetime object with timezone object as an argument.
12345678910111213
# Load libraries and classes import pytz from pytz import timezone from datetime import datetime tz = timezone('Europe/Warsaw') # save timezone dt = datetime(2021, 11, 1, 11, 25, 0) # create datetime object dt_tz = dt.astimezone(tz) # convert dt to tz timezone # Testing print(f"Saved time: {dt}") print(f"Converted time: {dt_tz}")
copy

There you can see a clear difference: not only timezone was saved, but time was adjusted to timezone. Let's try to complete the task! Please note, that code is compiling on a dedicated server with GMT.

Task

Use the second approach to convert datetime object with a current timestamp to tz timezone:

  1. Set 'America/New_York' timezone in tz.
  2. Apply method to datetime object now to convert it to tz timezone.

Switch to desktop for real-world practiceContinue from where you are using one of the options below
Section 3. Chapter 4
Switch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt