- Get link
- Other Apps
- Get link
- Other Apps
The astimezone() method:
- The astimezone() method adds a new timezone on the given Datetime and returns the Datetime with the new timezone.UTC.
Below Code Explanation:
- In the below code, we import the datetime, timezone, and timedelta classes from the datetime module.- We create a timezone object at UTC = (05:30) through the timedelta() class in tz1 named variable.
- We create a new timezone object at UTC = (04:30) through the timedelta() class in tz2 named variable.
- We create a datetime object at random datetime with the "tzinfo=" class which is access the timezone.UTC of tz1 named variable.
- We create a datetime object at random datetime with the "tzinfo=" class which is access the timezone.UTC of tz1 named variable.
- In the 1st print, we got the datetime with a timezone.UTC of tz1 named variable. (See in the Output)
- In the 2nd print, we got the datetime with a new timezone.UTC of tz2 named variable through the astimezone() method. (See in the Output)
- In the 3rd print, we got the datetime with the timezone.UTC (Zero UTC offset) through the astimezone() method. (See in the Output)
Output:
The replace() method:
- The replace() method adds a new timezone on the given Datetime and returns the Datetime with the new timezone.UTC.
- The "tzinfo=" class is required as an argument in replace() method. (See in the below Code)
Below Code Explanation:
- In the below code, we import the datetime, timezone, and timedelta classes from the datetime module.- We create a timezone object at UTC = (05:30) through the timedelta() class in tz1 named variable.
- We create a new timezone object at UTC = (04:30) through the timedelta() class in tz2 named variable.
- We create a datetime object at random datetime with the "tzinfo=" class which is access the timezone.UTC of tz1 named variable.
- We create a datetime object at random datetime with the "tzinfo=" class which is access the timezone.UTC of tz1 named variable.
- In the 1st print, we got the datetime with a timezone.UTC of tz1 named variable. (See in the Output)
- In the 2nd print, we got the datetime with a new timezone.UTC of tz2 named variable through replace() method. (See in the Output)
- In the 3rd print, we got the datetime with the timezone.UTC (Zero UTC offset) through replace() method. (See in the Output)
- Get link
- Other Apps
Comments
Post a Comment