最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python Datetime模塊和Calendar模塊用法實例分析

 更新時間:2019年04月15日 09:46:35   作者:微信1257309054  
這篇文章主要介紹了Python Datetime模塊和Calendar模塊用法,結合實例形式分析了Python日期時間及日歷相關的Datetime模塊和Calendar模塊原理、用法及操作注意事項,需要的朋友可以參考下

本文實例講述了Python Datetime模塊和Calendar模塊用法。分享給大家供大家參考,具體如下:

datetime模塊

1.1 概述

datetime比time高級了不少,可以理解為datetime基于time進行了封裝,提供了更多的實用的函數(shù),datetime的接口更加的直觀,更容易調用

1.2 模塊中的類

datetime:同時有時間與日期
timedelta:表示時間間隔,即兩個時間點的間隔:主要用于計算時間的跨度
tzinfo: 時區(qū)相關的信息
date : 只關注日期

2、獲取系統(tǒng)當前時間

先導入模塊:

import datetime
t1 = datetime.datetime.now()
print(t1)

輸出:

2018-04-11 19:52:06.180339

3、獲取指定時間

time2 = datetime.datetime(2018, 3, 28, 21, 59, 7, 95015)
print(time2)
print(type(time2))

輸出:

2018-03-28 21:59:07.095015
<class 'datetime.datetime'>

4、將時間轉為字符串

time1 = datetime.datetime.now()
time3 = time1.strftime("%Y-%m-%d")
print(time3)

輸出:

2018-04-11

5、時間相減,返回一個時間間隔的對象

import datetime
import time
time1 = datetime.datetime.now()
time.sleep(3)
time2 = datetime.datetime.now()
time3 = time2 -time1
print(time1)
print(time2)
print(time3)
print(type(time3))
#間隔天數(shù)
print(time3.days)
# 間隔天數(shù)之外的時間轉為秒
print(time3.seconds)

輸出:

2018-04-11 20:06:11.439085
2018-04-11 20:06:14.440052
0:00:03.000967
<class 'datetime.timedelta'>
0
3

calendar模塊

1、calendar模塊有很廣泛的方法用來處理年歷和月歷

導入模塊

import calendar

2、calendar.month(year.month)

返回指定年月的日歷【字符串類型】

print(calendar.month(2018,4))
print(type(calendar.month(2018,4)))

輸出:

     April 2018
Mo Tu We Th Fr Sa Su
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30

<class 'str'>

3、calendar.calendar(year)

返回指定年的日歷【字符串類型】

4、calendar.firstweekday()

返回當前每周起始日期的設置

print(calendar.firstweekday())

輸出:

0

5、calendar.isleap(year)

返回指定的年份是否為閏年,若是返回True,否則返回False

print(calendar.isleap(2016))

輸出:

True

6、calendar.leapdays(year1,year2)

返回[year1,year2)之間閏年的總和。

print(calendar.leapdays(2000,2020))

輸出:

5

7、calendar.monthrange(year,month)

返回一個元組(參數(shù)一,參數(shù)二)
參數(shù)一:當月的天數(shù)
參數(shù)二:當月第一天的日期碼[0,6][周一,周日]

print(calendar.monthrange(2018,1))
print(calendar.monthrange(2018,2))
print(calendar.monthrange(2018,3))
print(calendar.monthrange(2018,4))

輸出:

(0, 31)
(3, 28)
(3, 31)
(6, 30)

8、calendar.monthlendar(year,month)

返回指定月份以每一周為元素的一個二維列表。

print(calendar.monthcalendar(2018,4))

輸出:

[[0, 0, 0, 0, 0, 0, 1], [2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15], [16, 17, 18, 19, 20, 21, 22], [23, 24, 25, 26, 27, 28, 29], [30, 0, 0, 0, 0, 0, 0]]

9、calendar.weekday(year,month,day)

返回指定日期的日期碼。

print(calendar.weekday(2018,4,1))

輸出:

6

9、獲取凌晨零點到23:59的時間

now = time.time()
midnight = now - (now % 86400) + time.timezone
pre_midnight = midnight - 86400
now_midnight = midnight - 1
start_time = datetime.datetime.strptime(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(pre_midnight)),
                  "%Y-%m-%d %H:%M:%S")
end_time = datetime.datetime.strptime(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(now_midnight)),
                 "%Y-%m-%d %H:%M:%S")

PS:這里再為大家推薦幾款關于日期與天數(shù)計算的在線工具供大家使用:

在線日期/天數(shù)計算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi

在線萬年歷日歷:
http://tools.jb51.net/bianmin/wannianli

在線陰歷/陽歷轉換工具:
http://tools.jb51.net/bianmin/yinli2yangli

Unix時間戳(timestamp)轉換工具:
http://tools.jb51.net/code/unixtime

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python日期與時間操作技巧總結》、《Python數(shù)學運算技巧總結》、《Python數(shù)據(jù)結構與算法教程》、《Python Socket編程技巧總結》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》及《Python入門與進階經(jīng)典教程

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

相關文章

最新評論

万山特区| 南丹县| 海原县| 怀宁县| 尼木县| 阜新市| 云南省| 交城县| 远安县| 邛崃市| 上高县| 玉田县| 永平县| 镇安县| 汨罗市| 兴宁市| 开远市| 濮阳市| 宿迁市| 琼中| 甘南县| 孙吴县| 桐乡市| 沁水县| 女性| 高陵县| 长宁县| 昭觉县| 凤阳县| 水城县| 鄯善县| 南宫市| 穆棱市| 宣汉县| 正定县| 武宣县| 双柏县| 武威市| 高邮市| 巫溪县| 长白|