create account

Python中的日期/时间处理 by aafeng

View this thread on: hive.blogpeakd.comecency.com
· @aafeng ·
$10.90
Python中的日期/时间处理
在写代码的时候有很多场合需要处理日期和时间,比如:在日志中需要记录当前时间,在处理订单的时候需要处理用户下单的时间,在Hive上的各种点赞机器人需要知道新帖子的创建时间,以及帖子的“年龄“。。。在Python中提供了非常强大的日期/时间处理模块:time和datetime,这些模块中又有很多有用的函数。下面把我经常使用的函数做一总结,欢迎大家指正。
![](https://images.hive.blog/1280x0/https://images.hive.blog/DQmPSoZ1PRToUoJwd9awj9XaGKf9pYnKDsRkkVjzUEgtc9K/image.png)

## time模块

time模块提供了和时间相关的一些类及函数。

### 时间偏移量表示法

在Python交互式窗口中输入time.time(),其输出为一个数字:

![image](https://user-images.githubusercontent.com/63479035/83278821-14a55000-a1cc-11ea-98d5-1dd0aadb4248.png)

这个数字代表了从1970年1月1日00:00:00至今的秒偏移量。

将其转换为字符串格式:

![image](https://user-images.githubusercontent.com/63479035/83278933-41596780-a1cc-11ea-8927-c20cc9f800fc.png)

### 元组表示法

还有一种时间的表示法:使用包含9个整数的元组分别表示年,月,日,时,分,秒等:

![image](https://user-images.githubusercontent.com/63479035/83278369-6a2d2d00-a1cb-11ea-855c-b0eb3513c761.png)

可以把这个元组转换为第一种表示法:

![image](https://user-images.githubusercontent.com/63479035/83278291-4538ba00-a1cb-11ea-9f1c-1cf71f0b6964.png)

### 格式化字符串表示法

这个时间还可以表示为我们熟悉的格式:

![image](https://user-images.githubusercontent.com/63479035/83278215-233f3780-a1cb-11ea-8f22-4eb83349238a.png)    

### 时间的加减法

比如,计算一个小时之后的时间:

![image](https://user-images.githubusercontent.com/63479035/83278106-f723b680-a1ca-11ea-8fd4-c2e2b5aea672.png)

## datetime模块

从 datetime 模块中,依次导入date,datetime,timedelta。

    from datetime import date, datetime, timedelta

### 获取日期

获取当前日期:

![image](https://user-images.githubusercontent.com/63479035/83279289-cfcde900-a1cc-11ea-8261-3ec8beabeaef.png)

### 日期时间和字符串转换

将日期转换为字符串;

![image](https://user-images.githubusercontent.com/63479035/83304054-0d933780-a1f6-11ea-80ee-cf50b1c46643.png)

将日期时间字符串转换为日期时间(datetime)类型:


![image](https://user-images.githubusercontent.com/63479035/83304510-e4bf7200-a1f6-11ea-8c1d-00a087064f0e.png)

### 计算时间差

![image](https://user-images.githubusercontent.com/63479035/83305023-d756b780-a1f7-11ea-8a13-af5aabfe4060.png)

获取明天和昨天的时间:

![image](https://user-images.githubusercontent.com/63479035/83308608-0c1a3d00-a1ff-11ea-8f64-d1fee939508c.png)

得到Hive文章的发布时间后,计算从文章发布时间到现在时间的秒数:

![image](https://user-images.githubusercontent.com/63479035/83308289-68c92800-a1fe-11ea-8f6d-33e40dc305e5.png)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authoraafeng
permlink3teaom-python
categoryhive-105017
json_metadata{"tags":["cn","cn-reader","cn-curation","cn-programming","python"],"image":["https://images.hive.blog/1280x0/https://images.hive.blog/DQmPSoZ1PRToUoJwd9awj9XaGKf9pYnKDsRkkVjzUEgtc9K/image.png","https://user-images.githubusercontent.com/63479035/83278821-14a55000-a1cc-11ea-98d5-1dd0aadb4248.png","https://user-images.githubusercontent.com/63479035/83278933-41596780-a1cc-11ea-8927-c20cc9f800fc.png","https://user-images.githubusercontent.com/63479035/83278369-6a2d2d00-a1cb-11ea-855c-b0eb3513c761.png","https://user-images.githubusercontent.com/63479035/83278291-4538ba00-a1cb-11ea-9f1c-1cf71f0b6964.png","https://user-images.githubusercontent.com/63479035/83278215-233f3780-a1cb-11ea-8f22-4eb83349238a.png","https://user-images.githubusercontent.com/63479035/83278106-f723b680-a1ca-11ea-8fd4-c2e2b5aea672.png","https://user-images.githubusercontent.com/63479035/83279289-cfcde900-a1cc-11ea-8261-3ec8beabeaef.png","https://user-images.githubusercontent.com/63479035/83304054-0d933780-a1f6-11ea-80ee-cf50b1c46643.png","https://user-images.githubusercontent.com/63479035/83304510-e4bf7200-a1f6-11ea-8c1d-00a087064f0e.png","https://user-images.githubusercontent.com/63479035/83305023-d756b780-a1f7-11ea-8a13-af5aabfe4060.png","https://user-images.githubusercontent.com/63479035/83308608-0c1a3d00-a1ff-11ea-8f64-d1fee939508c.png","https://user-images.githubusercontent.com/63479035/83308289-68c92800-a1fe-11ea-8f6d-33e40dc305e5.png"],"app":"hiveblog/0.1","format":"markdown"}
created2020-06-01 15:04:54
last_update2020-06-01 15:04:54
depth0
children1
last_payout2020-06-08 15:04:54
cashout_time1969-12-31 23:59:59
total_payout_value5.550 HBD
curator_payout_value5.354 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,162
author_reputation554,723,599,569,926
root_titlePython中的日期/时间处理
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id97,712,336
net_rshares23,187,283,501,976
author_curate_reward""
vote details (60)
@gitplait ·
Hey @aafeng, thanks for sharing this useful tutorial. We appreciate your work, and we will feature this post in Today's GitPlait elite. 


<sup>You are and elite</sup>
properties (22)
authorgitplait
permlinkqbaij4
categoryhive-105017
json_metadata{"users":["aafeng"],"app":"hiveblog/0.1"}
created2020-06-02 08:35:30
last_update2020-06-02 08:35:30
depth1
children0
last_payout2020-06-09 08:35:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length167
author_reputation911,220,543,569
root_titlePython中的日期/时间处理
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id97,726,345
net_rshares0