Python arrow module make it easier for you to manipulate date time in python source code. It is much easier to use and user friendly than python built-in date time module. This article will tell you how to install python arrow module and how to use it to manipulate date time in python.
1. Install Python Arrow Module Steps.
Python arrow module is not a built-in module, so you need to install it to use. Below is the install steps.
- Open a terminal and run command
pip3 install arrow
( linux or macOS ) orpip install arrow
( windows ).$ pip3 install arrow Collecting arrow Downloading arrow-0.16.0-py2.py3-none-any.whl (50 kB) |████████████████████████████████| 50 kB 46 kB/s Requirement already satisfied: python-dateutil>=2.7.0 in ./opt/anaconda3/envs/env_ipython_example/lib/python3.7/site-packages (from arrow) (2.8.1) Requirement already satisfied: six>=1.5 in ./opt/anaconda3/envs/env_ipython_example/lib/python3.7/site-packages (from python-dateutil>=2.7.0->arrow) (1.15.0) Installing collected packages: arrow Successfully installed arrow-0.16.0
- After you install it successfully, run
pip3 show arrow
to confirm it has been installed correctly.$ pip3 show arrow Name: arrow Version: 0.16.0 Summary: Better dates & times for Python Home-page: https://arrow.readthedocs.io Author: Chris Smith Author-email: [email protected] License: Apache 2.0 Location: /Users/songzhao/opt/anaconda3/envs/env_ipython_example/lib/python3.7/site-packages Requires: python-dateutil Required-by:
2. Use Python Arrow Module In Python Source Code.
- Import arrow module in python source code first.
import arrow
3. Use Python Arrow Module To Get Date Time.
-
Get current UTC time.
>>> curr_utc_time = arrow.utcnow() >>> print('Current utc time : ', curr_utc_time) Current utc time : 2020-09-23T02:31:07.925181+00:00
-
Convert current UTC time to local time.
>>> curr_utc_time = arrow.utcnow() >>> utc_time_to_local = curr_utc_time.to('local') >>> print('Current local time : ', utc_time_to_local) Current local time : 2020-09-23T10:32:55.738437+08:00
-
Convert current utc time to special region time.
>>> curr_utc_time = arrow.utcnow() >>> utc_time_to_local = curr_utc_time.to('US/Pacific') >>> print('Current utc time to US/Pacific time : ', utc_time_to_local) Current utc time to US/Pacific time : 2020-09-22T19:31:07.925181-07:00
-
Get current local time.
>>> now_time = arrow.now() >>> now_timestamp = now_time.timestamp >>> print('Current local time : ', now_time, ', timestamp is :', now_timestamp) Current local time : 2020-09-23T10:40:38.245387+08:00 , timestamp is : 1600828838
-
Get special time zone region time.
>>> now_time = arrow.now('US/Pacific') >>> print('Current time of US/Pacific time zone : ', now_time) Current time of US/Pacific time zone : 2020-09-22T19:43:58.538255-07:00
-
Convert above region time to UTC time.
>>> now_time = arrow.now('US/Pacific') >>> now_time_to_utc = now_time.to('UTC') >>> print('Current time of US/Pacific time zone to UTC : ', now_time_to_utc) Current time of US/Pacific time zone to UTC : 2020-09-23T02:43:58.538255+00:00
-
Get a date time directly by arrow.get method, pass argument year, month, day, hour, minute and second value.
>>> date_time_1 = arrow.get(2018, 10, 1, 19, 19, 16) >>> print('Date time 1 is : ', date_time_1) Date time 1 is : 2018-10-01T19:19:16+00:00
-
Get a date time use arrow.Arrow method, input year, month, day, hour, minute etc.
>>> date_time_2 = arrow.Arrow(year=2020, month=9, day=23, hour=9, minute=10, second=12, microsecond=6789, tzinfo='Asia/shanghai') >>> print('Date time 2 is : ', date_time_2) Date time 2 is : 2020-09-23T09:10:12.006789+08:00
- Get date time by timestamp value.
Example 1: >>> import time >>> curr_utc_time_number = time.time() >>> print('Current utc time number ', curr_utc_time_number) Current utc time number 1600830926.296628 >>> curr_utc_time_str = arrow.get(curr_utc_time_number) >>> print('Current utc time : ', curr_utc_time_str) Current utc time : 2020-09-23T03:15:26.296628+00:00 Example 2: >>> curr_utc_time = arrow.utcnow() >>> >>> curr_utc_timestamp = curr_utc_time.timestamp >>> >>> date_time = arrow.Arrow.fromtimestamp(curr_utc_timestamp) >>> >>> print("date_time = ", date_time) date_time = 2020-09-23T11:34:53+08:00
-
Get date time from a string by time format.
>>> time_string = '2020-09-26 06:06:06' >>> time_string_format = 'YYYY-MM-DD HH:mm:ss' >>> date_time_1 = arrow.get(time_string, time_string_format) >>> print(date_time_1) 2020-09-26T06:06:06+00:00 >>> print(date_time_1.year, date_time_1.month, date_time_1.day, date_time_1.hour, date_time_1.minute, date_time_1.second) 2020 9 26 6 6 6
4. Format Date Time To String.
- Get date time year string.
>>> curr_local_time = arrow.now() >>> >>> curr_year = curr_local_time.format('YYYY') >>> >>> print("Year: ", curr_year) Year: 2020
- Get year, month and day string.
>>> curr_local_time = arrow.now() >>> >>> curr_date = curr_local_time.format('YYYY-MM-DD') >>> >>> print("Date:", curr_date) Date: 2020-09-23
- Get year, month, day, hour, minute and seconds string.
>>> curr_local_time = arrow.now() >>> >>> curr_date_time = curr_local_time.format('YYYY-MM-DD HH:mm:ss') >>> >>> print("Date and time: ", curr_date_time) Date and time: 2020-09-23 11:40:05
- Get date time string with time zone.
>>> curr_local_time = arrow.now() >>> >>> curr_date_time_zone = curr_local_time.format('YYYY-MM-DD HH:mm:ss ZZ') >>> >>> print("Date and time and time zone: ", curr_date_time_zone) Date and time and time zone: 2020-09-23 11:40:05 +08:00
5. Shift Date Time.
- Shift year in date time.
now = arrow.now() >>> >>> two_years_ago = now.shift(years=-2) >>> >>> print("Two years ago is ", two_years_ago) Two years ago is 2018-09-23T11:59:02.897196+08:00 >>> >>> two_years_ago_other_format = two_years_ago.format('YYYY-MM-DD') >>> >>> print("Two years ago string format is ", two_years_ago_other_format) Two years ago string format is 2018-09-23
- Shift month in date time.
>>> now = arrow.now() >>> >>> one_month_later = now.shift(months=1) >>> >>> print("One month later is ", one_month_later) One month later is 2020-10-23T11:59:02.897196+08:00
- Shift day in date time.
>>> now = arrow.now() >>> >>> six_day_later = now.shift(days=6) >>> >>> print("Six days later is ", six_day_later) Six days later is 2020-09-29T11:59:02.897196+08:00
- Shift year, month, day, hour, minute and seconds.
>>> now = arrow.now() >>> >>> custom_shift_time = now.shift(years=-1, months=3, days=-6, hours=2, minutes=9, seconds=3) >>> >>> print("Custom shift time is ", custom_shift_time) Custom shift time is 2019-12-17T14:08:05.897196+08:00
- Shift weekday in date time. It will return date greater than the source date.
>>> now = arrow.now() >>> >>> shift_weekday = now.shift(weekday=1) >>> >>> print("Shift weekday to weekday 1 : ", shift_weekday) Shift weekday to weekday 1 : 2020-09-29T11:59:02.897196+08:00
6. Get Date Time Weekday.
>>> now = arrow.now() >>> >>> weekday_number = now.weekday() >>> >>> print('Weekday number is ', weekday_number) Weekday number is 2 >>> >>> weekday_str = now.format('dddd') >>> >>> print('Today is ', weekday_str) Today is Wednesday
7. Date Time Humanize.
- A year ago.
>>> now = arrow.now() >>> >>> one_year_ago = now.shift(years=-1) >>> >>> one_year_ago_humanize = one_year_ago.humanize() >>> >>> print(one_year_ago_humanize) a year ago
- 11 month ago.
>>> now = arrow.now() >>> >>> date = now.shift(years=-1, months=1) >>> >>> date_humanize = date.humanize() >>> >>> print(date_humanize) 11 months ago
-
2 weeks ago.
>>> now = arrow.now() >>> >>> date = now.shift(months=-1, days=15, hours=2, minutes=1, seconds=1) >>> >>> date_humanize = date.humanize() >>> >>> print(date_humanize) 2 weeks ago
8. Daylight Saving Time.
- Get special time zone region daylight saving time.
>>> now = now.to('US/Pacific') >>> >>> now_str = now.format('YYYY-MM-DD HH:mm:ss ZZ') >>> >>> print('US/Pacific date time : ', now_str) US/Pacific date time : 2020-09-22 21:30:14 -07:00 >>> >>> now_dst = now.dst() >>> >>> print('US/Pacific daylight saving time : ', now_dst) US/Pacific daylight saving time : 1:00:00
- Get local daylight saving time.
>>> now = arrow.now() >>> >>> now_str = now.format('YYYY-MM-DD HH:mm:ss ZZ') >>> >>> print('Current date time : ', now_str) Current date time : 2020-09-23 12:31:12 +08:00 >>> >>> now_dst = now.dst() >>> >>> print('Current daylight saving time : ', now_dst) Current daylight saving time : 0:00:00