w3resource
Python Arrow Module Exercises

Python Arrow Module: Replace hour, minute, day, month, year and timezone with specified value of current datetime using arrow

Arrow Module: Exercise-8 with Solution

Write a Python program to replace hour, minute, day, month, year and timezone with specified value of current datetime using arrow.

Sample Solution:

Python Code:

import arrow
a = arrow.utcnow()
print("Current date and time:")
print(a)
print("\nReplace hour and minute with 5 and 35:")
print(a.replace(hour=5, minute=35))
print("\nReplace day with 2:")
print(a.replace(day=2))
print("\nReplace year with 2021:")
print(a.replace(year=2021))
print("\nReplace month with 11:")
print(a.replace(month=11)) 
print("\nReplace timezone with 'US/Pacific:")
print(a.replace(tzinfo='US/Pacific'))

Sample Output:

Current date and time:
2019-06-01T08:52:18.063800+00:00

Replace hour and minute with 5 and 35:
2019-06-01T05:35:18.063800+00:00

Replace day with 2:
2019-06-02T08:52:18.063800+00:00

Replace year with 2021:
2021-06-01T08:52:18.063800+00:00

Replace month with 11:
2019-11-01T08:52:18.063800+00:00

Replace timezone with 'US/Pacific:
2019-06-01T08:52:18.063800-07:00

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to get date and time properties from datetime function using arrow module.
Next: Write a Python program to convert timezone from local to utc, utc to local or specified zones.

What is the difficulty level of this exercise?