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

作者:微信1257309054 时间:2022-08-05 10:23:46 

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

来源:https://blog.csdn.net/lm_is_dc/article/details/80099923

标签:Python,Datetime模块,Calendar模块
0
投稿

猜你喜欢

  • python关键字传递参数实例分析

    2023-08-24 04:28:34
  • Python使用Tkinter实现滚动抽奖器效果

    2023-04-07 03:05:45
  • JS 去前后空格大全(IE9亲测)

    2013-08-22 13:01:43
  • Python中json库的操作指南

    2021-10-11 11:04:43
  • Python列表删除元素del、pop()和remove()的区别小结

    2021-12-02 07:32:41
  • SQL Server开发过程中的的常见问题总结

    2009-01-06 11:16:00
  • Python大数据量文本文件高效解析方案代码实现全过程

    2023-01-18 04:57:01
  • tensorflow将图片保存为tfrecord和tfrecord的读取方式

    2022-01-22 11:32:04
  • 如何使用微信公众平台开发模式实现多客服

    2023-11-14 17:48:37
  • PHP模板引擎Smarty的缓存使用总结

    2023-11-15 09:55:12
  • javabean servlet jsp实现分页功能代码解析

    2023-06-13 15:21:24
  • 如何用MySQL-Front远程连接MySql?

    2010-12-03 16:02:00
  • Python 发送邮件方法总结

    2022-04-05 21:17:06
  • 扫盲大讲堂:mysql出错的代码解析及解答

    2009-09-05 10:08:00
  • Python脚本操作Excel实现批量替换功能

    2023-06-15 16:37:57
  • 网站导航设计的6大分类

    2010-07-12 18:46:00
  • python 如何获取文件夹中的全部文件

    2022-09-10 16:48:11
  • python实现复制大量文件功能

    2023-09-03 16:42:54
  • python 将字符串中的数字相加求和的实现

    2022-11-03 22:31:08
  • Python使用grequests并发发送请求的示例

    2022-11-08 15:38:01
  • asp之家 网络编程 m.aspxhome.com