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

yipeiwu_com7年前Python基础

本文实例讲述了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程序设计有所帮助。

相关文章

使用Python做定时任务及时了解互联网动态

使用Python做定时任务及时了解互联网动态

前言 本人因为比较喜欢看漫画和动漫, 所以总会遇到一些问题, 因为订阅的漫画或者动漫太多, 总会忘记自己看到那一章节或者不知道什么时候更新. 故会有这么一个需求, 想记录自己想看的漫画或...

用python实现批量重命名文件的代码

下面是最终代码 (windows下实现的) 复制代码 代码如下: # -*- coding: cp936 -*- import os path = 'D:\\图片\\' for file...

无法使用pip命令安装python第三方库的原因及解决方法

无法使用pip命令安装python第三方库的原因及解决方法

再dos中无法使用pip,命令主要是没有发现这个命令。我们先找到这个命令的位置,一般是在python里面的Scripts文件夹里面。我们可以把dos切换到对应的文件夹,再使用pip命令就...

Python实现的登录验证系统完整案例【基于搭建的MVC框架】

Python实现的登录验证系统完整案例【基于搭建的MVC框架】

本文实例讲述了Python实现的登录验证系统。分享给大家供大家参考,具体如下: 小型登录注册验证系统 一、概述 ​ 使用Redis+MySQL数据库实现一个小型的登录注册验证...

关于Pytorch MaxUnpool2d中size操作方式

关于Pytorch MaxUnpool2d中size操作方式

下图所示为最大值的去池化操作,主要包括三个参数,kernel_size: 卷积核大小(一般为3,即3x3的卷积核), stride:步,还有一个新的size。 从图中可以看出,它将维度4...