﻿ Python Arrow Module: Replace hour, minute, day, month, year and timezone with specified value of current datetime using arrow - 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.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

﻿

## Python: Tips of the Day

Access an Element in a Sequence Using the Reverse Index:

```>>> a = 'Hello World!'
>>> # instead of using a[len(a)-1]
>>> a[-1]
'!'
>>> # in combination with slicing
>>> a[-5:-1]
'orld'
```