您的位置:首页 > 编程语言 > Python开发

Python Datetime模块和Calendar模块用法实例分析

2019-04-15 18:02 951 查看

本文实例讲述了Python Datetime模块和Calendar模块用法。分享给大家供大家参考,具体如下:

datetime模块

1.1 概述

datetime比time高级了不少,可以理解为datetime基于time进行了封装,提供了更多的实用的函数,datetime的接口更加的直观,更容易调用

1.2 模块中的类

datetime:同时有时间与日期
timedelta:表示时间间隔,即两个时间点的间隔:主要用于计算时间的跨度
tzinfo: 时区相关的信息
date : 只关注日期

2、获取系统当前时间

先导入模块:

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))
#间隔天数
print(time3.days)
# 间隔天数之外的时间转为秒
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)

返回一个元组(参数一,参数二)
参数一:当月的天数
参数二:当月第一天的日期码[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:这里再为大家推荐几款关于日期与天数计算的在线工具供大家使用:

在线日期/天数计算器:
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数学运算技巧总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: