The packet processing time Python datetime and arrow

The packet processing time Python datetime and arrow

 

Get points when the shells used the time handler, you want to get time last month, including the year, month, date, etc.

# 方法一:
today = datetime.date.today()  # 1. 获取「今天」
first = today.replace(day=1)   # 2. 获取当前月的第一天
last_month = first - datetime.timedelta(days=1)  # 3. 减一天,得到上个月的最后一天
print(last_month.strftime("%Y%m"))  # 4. 格式化成指定形式

# 方法二:
today = datetime.date.today()  # 1. 获取「今天」
last_month = today.replace(month=today.month - 1)  # 2.获取前一个月
print(last_month.strftime("%Y%m"))  # 3. 格式化成指定形式

# 方法三: arrow包的使用(pip install arrow)
a = arrow.now()  # 当前本地时间
print(a.timestamp)
print(a.year)
print(a.month)
print(a.day)
print(a.date())
print(a.time())
print(a.shift(months=-4).format("YYYYMM"))
print(a.shift(months=1).format("YYYYMM"))
print(a.shift(hours=1))

#  生成arrow对象
print(arrow.get(1535113845))
print(arrow.get(datetime.date(2018, 7, 24)))
print(arrow.get("2018-08-11 12:30:56"))

Results are as follows:

# 方法一
201906

# 方法二
201906

# 方法三
1562329178
2019
7
5
2019-07-05
20:19:38.573000
201903
201908
2019-07-05T21:19:38.573000+08:00
2018-08-24T12:30:45+00:00
2018-07-24T00:00:00+00:00
2018-08-11T12:30:56+00:00

I would like to be compatible by a method n case is extremely difficult, internal implementation will be very complicated, as the user must also use a lot of confusion, we need to select the most suitable package according to their own business scenarios for processing.

Guess you like

Origin blog.csdn.net/yanjiangdi/article/details/94761074