Python-Arrow time processing package

Time and date library: Arrow

For a detailed introduction of this library, please refer to its official website link: https://arrow.readthedocs.io/en/latest/ arrow official website api
https://github.com/crsmithdev/arrow arrow's GitHub hosting address
http:// www.iplaypy.com/module/m111.html Third-party time and date library Python Arrow module

installation

$pip install arrow

Case study

import arrow

a = arrow.now()

print(a)

Get the year, month, day, hour, minute, and second of the arrow object

print(a.year)
print(a.month)
print(a.day)
print(a.hour)

Next, I will introduce some functions that Arrow often uses

shift, this method can add and subtract shift operations left and right, the objects of addition and subtraction can be year, month, day, hour, minute, and week

Get the previous month of the current month, the next month is months=+1, the plus sign can be ignored

a.shift(months=-1)
a.shift(months=-1).format("YYYYMM")
a.shift(years=+1) 
a.shift(hours=+1)
a.shift(weeks=+1)

Of course, if you want to specify the year of the modification, you can use the replace function.

a.replace(year=1,month=3)

format

format is a formatting tool that can convert arrow objects into string format according to the specified format

a.format("YYYY-MM-DD HH:mm:ss")

Construct the Arrow object

It was introduced earlier that arrow can be converted into datetime, str, date, time, and timestamp, so how to construct an Arrow object? In addition to using the now() and utcnow() methods, you can also use the get factory method, or use the Arrow construction method to directly specify the year, month, day, hour, minute, and second

import datetime

get, the second way is to use the get method to create the arrow object, the get method is very flexible

## 接受时间戳参数
arrow.get(1535113845)
## 接受一个datetime对象
arrow.get(datetime(2018,8,24))
## 接受一个date对象
from datetime import date
arrow.get(date(2018,7,24))
## 接受日期格式的字符串
arrow.get("2018-08-11 12:30:56")
## 接收日期字符串,并指定格式
arrow.get("18-08-11 12:30:56", "YY-MM-DD HH:mm:ss")

Guess you like

Origin blog.csdn.net/zhonglongshen/article/details/113851287