在写代码的时候有很多场合需要处理日期和时间,比如:在日志中需要记录当前时间,在处理订单的时候需要处理用户下单的时间,在Hive上的各种点赞机器人需要知道新帖子的创建时间,以及帖子的“年龄“。。。在Python中提供了非常强大的日期/时间处理模块:time和datetime,这些模块中又有很多有用的函数。下面把我经常使用的函数做一总结,欢迎大家指正。  ## time模块 time模块提供了和时间相关的一些类及函数。 ### 时间偏移量表示法 在Python交互式窗口中输入time.time(),其输出为一个数字:  这个数字代表了从1970年1月1日00:00:00至今的秒偏移量。 将其转换为字符串格式:  ### 元组表示法 还有一种时间的表示法:使用包含9个整数的元组分别表示年,月,日,时,分,秒等:  可以把这个元组转换为第一种表示法:  ### 格式化字符串表示法 这个时间还可以表示为我们熟悉的格式:  ### 时间的加减法 比如,计算一个小时之后的时间:  ## datetime模块 从 datetime 模块中,依次导入date,datetime,timedelta。 from datetime import date, datetime, timedelta ### 获取日期 获取当前日期:  ### 日期时间和字符串转换 将日期转换为字符串;  将日期时间字符串转换为日期时间(datetime)类型:  ### 计算时间差  获取明天和昨天的时间:  得到Hive文章的发布时间后,计算从文章发布时间到现在时间的秒数: 
author | aafeng |
---|---|
permlink | 3teaom-python |
category | hive-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"} |
created | 2020-06-01 15:04:54 |
last_update | 2020-06-01 15:04:54 |
depth | 0 |
children | 1 |
last_payout | 2020-06-08 15:04:54 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 5.550 HBD |
curator_payout_value | 5.354 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 2,162 |
author_reputation | 554,723,599,569,926 |
root_title | Python中的日期/时间处理 |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 97,712,336 |
net_rshares | 23,187,283,501,976 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
abit | 0 | 7,006,518,571,081 | 30% | ||
gerber | 0 | 26,806,335,033 | 0.39% | ||
ezzy | 0 | 30,177,285,724 | 0.39% | ||
livingfree | 0 | 143,693,068,238 | 1% | ||
exyle | 0 | 37,197,533,928 | 0.39% | ||
ffcrossculture | 0 | 3,061,752,250 | 1% | ||
sweetsssj | 0 | 13,547,047,142,561 | 33% | ||
someguy123 | 0 | 5,493,599,198 | 0.39% | ||
created | 0 | 167,405,535,814 | 1% | ||
bring | 0 | 65,719,405,831 | 100% | ||
dune69 | 0 | 1,439,259,449 | 0.39% | ||
aleister | 0 | 10,041,625,757 | 10% | ||
susanli3769 | 0 | 140,617,010,104 | 100% | ||
tykee | 0 | 6,814,831,410 | 50% | ||
mangoanddaddy | 0 | 3,039,537,180 | 80% | ||
aafeng | 0 | 217,795,320,679 | 100% | ||
shihabieee | 0 | 1,439,648,599 | 16.5% | ||
felander | 0 | 1,158,923,401 | 0.39% | ||
accelerator | 0 | 39,461,270,363 | 4% | ||
deathwing | 0 | 136,437,780 | 0.39% | ||
dancingapple | 0 | 4,729,792,282 | 20% | ||
xiaoshancun | 0 | 4,875,417,761 | 100% | ||
victory622 | 0 | 185,349,430,071 | 100% | ||
caladan | 0 | 1,180,013,231 | 0.39% | ||
emrebeyler | 0 | 7,924,613,964 | 0.35% | ||
lebin | 0 | 92,817,221,510 | 50% | ||
nealmcspadden | 0 | 10,198,376,384 | 0.39% | ||
maiyude | 0 | 1,732,395,836 | 50% | ||
bala41288 | 0 | 108,091,413,711 | 50% | ||
cadawg | 0 | 664,075,570 | 0.27% | ||
fishbb | 0 | 583,015,361 | 25% | ||
bestboom | 0 | 2,643,151,342 | 0.39% | ||
freddio | 0 | 33,273,994,077 | 15% | ||
digital.mine | 0 | 70,683,674,370 | 0.3% | ||
sabari18 | 0 | 7,956,584,283 | 30% | ||
dlike | 0 | 4,607,407,671 | 0.39% | ||
followjohngalt | 0 | 1,705,035,595 | 0.39% | ||
muntaharaceh | 0 | 5,297,726,257 | 100% | ||
tagalong | 0 | 2,529,126,306 | 100% | ||
aafeng.test | 0 | 551,497,359 | 100% | ||
map10k | 0 | 10,252,480,523 | 12% | ||
steem.leo | 0 | 2,756,561,110 | 0.38% | ||
leo.voter | 0 | 2,466,607,604 | 0.07% | ||
freddio.sport | 0 | 4,591,216,086 | 15% | ||
asteroids | 0 | 2,558,798,552 | 0.39% | ||
mapxv | 0 | 11,509,103,610 | 14% | ||
citrus-soap | 0 | 154,269,765 | 0.39% | ||
spt-skillup | 0 | 137,859,020,353 | 10% | ||
maxuvd | 0 | 1,340,744,597 | 0.39% | ||
btscn | 0 | 232,868,080,039 | 50% | ||
freedomteam2019 | 0 | 2,144,087,551 | 30% | ||
gerbo | 0 | 0 | 0.39% | ||
max.curation | 0 | 343,670,958,278 | 64.05% | ||
ribary | 0 | 75,308,333 | 0.19% | ||
cmdd | 0 | 12,517,505 | 100% | ||
mice-k | 0 | 1,425,451,069 | 0.39% | ||
drew0 | 0 | 788,538,384 | 5% | ||
steemcityrewards | 0 | 493,658,212 | 0.39% | ||
gitplait | 0 | 55,394,025,946 | 100% | ||
hive.curation | 0 | 374,464,017,138 | 14.41% |
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>
author | gitplait |
---|---|
permlink | qbaij4 |
category | hive-105017 |
json_metadata | {"users":["aafeng"],"app":"hiveblog/0.1"} |
created | 2020-06-02 08:35:30 |
last_update | 2020-06-02 08:35:30 |
depth | 1 |
children | 0 |
last_payout | 2020-06-09 08:35:30 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 167 |
author_reputation | 911,220,543,569 |
root_title | Python中的日期/时间处理 |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 97,726,345 |
net_rshares | 0 |