国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python編程開發之日期操作實例分析

2020-01-04 17:57:13
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了python編程開發之日期操作,以實例形式較為詳細的分析了Python中datetime與time庫的相關使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了python編程開發之日期操作。分享給大家供大家參考,具體如下:

在python中對日期進行操作的庫有:

import datetime

import time

對日期格式化信息,可以參考官方API:

time.strftime

datetime

下面是我做的demo:

 

 
  1. #datetime 
  2. import datetime 
  3. #當前日期 
  4. now = datetime.datetime.now() 
  5. print(now.strftime('%Y-%m-%d %H:%M:%S')) 
  6. print(now.strftime('%Y-%m-%d')) 
  7. #string convert to datetime 
  8. time_str = '2013-07-29 01:05:00' 
  9. str_convert_2_time = datetime.datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S'
  10. print(str_convert_2_time) 
  11. #比較兩個日期相差多少天 
  12. time_strA = '2013-07-29 01:05:00' 
  13. time_strB ='2013-08-29 01:05:00' 
  14. day = datetime.datetime.strptime(time_strA, '%Y-%m-%d %H:%M:%S'
  15. day2 = datetime.datetime.strptime(time_strB, '%Y-%m-%d %H:%M:%S'
  16. sub_day = day2 - day 
  17. print('{0}和{1}相差{2}天'.format(time_strA, time_strB, str(sub_day.days))) 
  18. #今后的n天的日期 
  19. n_days = 4 
  20. now = datetime.datetime.now() 
  21. my_date = datetime.timedelta(days=n_days)  
  22. n_day = now + my_date 
  23. print('從今天起的{0}天的日期是:'.format(n_days)) 
  24. print(n_day.strftime('%Y-%m-%d %H:%M:%S')) 

運行效果:

 

 
  1. Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32 
  2. Type "copyright""credits" or "license()" for more information. 
  3. >>> ================================ RESTART ================================ 
  4. >>>  
  5. 2013-07-29 01:48:16 
  6. 2013-07-29 
  7. 2013-07-29 01:05:00 
  8. 2013-07-29 01:05:00和2013-08-29 01:05:00相差31天 
  9. 從今天起的4天的日期是: 
  10. 2013-08-02 01:48:16 
  11. >>> 

希望本文所述對大家Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 孙吴县| 共和县| 缙云县| 眉山市| 崇左市| 烟台市| 洞口县| 修水县| 仁化县| 伊金霍洛旗| 平遥县| 伊吾县| 汶上县| 汕尾市| 宜兴市| 定远县| 安阳县| 祁东县| 巴林右旗| 古交市| 青龙| 金湖县| 凤冈县| 丰顺县| 隆化县| 万州区| 临桂县| 余江县| 读书| 柳州市| 沅陵县| 汉中市| 高尔夫| 镇巴县| 庆城县| 平度市| 普定县| 西安市| 东乌珠穆沁旗| 贵阳市| 荃湾区|