w3resource

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?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/arrow/python-arrow-exercise-8.php