time模块
时间的表示形式时间戳:以整型或浮点型表示⼀个时间,该时间以秒为单位,这个时间是以1970年1⽉1⽇0时0分0秒开始计算的。 导入time
import time
1、返回当前的时间戳
now = time.time()
print(now)
2、将时间戳转为时间元组(世界时间)
timeT = time.gmtime(now)
print(timeT)
3、将时间戳转换成本地时间
local = time.localtime(now)
print(local)
4、将本地时间转为时间戳
sec = time.mktime(local)
print(sec)
5、将时间元组转为字符串
strTime = time.asctime(local)
print(strTime)
6、将时间戳转为字符串
cTime=time.ctime(now)
print(cTime)
格式化的时间字符串:
  • %H 以24⼩时制表示当前⼩时[0-23]
  • %I 以12⼩时制表示当前⼩时[01-12]
  • %j 返回 当天是当年的第⼏天 范围[001,366]
  • %m 返回⽉份 范围[0,12]
  • %M 返回分钟数 范围 [0,59]
  • %P 返回是上午还是下午–AM or PM
  • %S 返回秒数 范围 [0,61]。。。⼿册说明的
  • %U 返回当周是当年的第⼏周,以周⽇为第⼀天
  • %W 返回当周是当年的第⼏周,以周⼀为第⼀天
  • %w 当天在当周的天数,范围为[0, 6],6表示星期天
  • %x ⽇期的字符串表示 :03/08/15
  • %X 时间的字符串表示 :23:22:08
  • %y 两个数字表示的年份 15
  • %Y 四个数字表示的年份 2015
  • %z 与utc时间的间隔 (如果是本地时间,返回空字符串)
  • %Z 时区名称(如果是本地时间,返回空字符串) 7、将时间元组转为指定的字符串
timestring=time.strftime("%Y--%m--%b",local)
print(timestring)
8、将事件字符串转为时间元组:time.strptime(时间字符串,时间格式)
timetuple=time.strptime(timestring,"%Y--%m--%b")
print(timetuple)
9、延迟时间: 将当前程序运⾏停顿:time.sleep(num)
time.sleep(3)
dateTime模块
datetime⽐time⾼级了不少,可以理解为datetime基于time进⾏了封装,提供了更多使⽤的函数,datetime模块的接⼝则更直观、更容易调⽤ 导入模块
import datetime
1、获取当前时间:datetime.datetime.now()
now=datetime.datetime.now()
print(now)
2、获取指定时间:datetime.datetime(年, ⽉, ⽇, 时, 分, 秒, 毫秒)
timeDate=datetime.datetime(2018,12,20,10,11,10, 1223)
print(timeDate)
3、将时间转为字符串:时间对象.strftime(⾃定义时间格式)
strTime=now.strftime("%y-%m-%d")
print(strTime)
4、将字符串转为时间类型:datetime.datetime.strptime(时间字符串, ⾃定义时间格式)
mytime=datetime.datetime.strptime(strTime,"%y-%m-%d")
print(mytime)
5、时间运算:时间相减
time1=datetime.datetime(2019,8,1,1,2,2)
time2=datetime.datetime(2019,8,5,1,8,2)
time3=time2-time1
print(time3)
# 获取间隔天数
print(time3.days)
# 获取除天数以外的秒数
print(time3.seconds)
calender模块
导入模块
import calendar
# • 1、返回指定的某年某⽉的⽇历:calendar.month(年, ⽉)
c1=calendar.month(2019,7)
print(c1)
# • 2、返回指定年分的⽇历:calendar.calendar(年)
print(calendar.calendar(2019))
# • 3、闰年返回True:calendar.isleap(年)  返回True False
print(calendar.isleap(2000))
print(calendar.isleap(2001))
# • 4、返回某个⽉的开始的第⼀天的星期的下标,和所有的天数:# calendar.monthrange(年, ⽉)
print(calendar.monthrange(2019,6))
# • 5、返回某个⽉以每周为元素列表:calendar.monthcalendar(年, ⽉)
print(calendar.monthcalendar(2019,6))

time模块/datetime模块/calendar模块的更多相关文章

  1. python日期与日历Datetime和Calendar模块

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

  2. Python学习总结14:时间模块datetime & time & calendar (一)

    Python中的常用于处理时间主要有3个模块datetime模块.time模块和calendar模块. 一.time模块 1. 在Python中表示时间的方式 1)时间戳(timestamp):通常来 ...

  3. time,datetime,calendar模块

    Python中,与时间有关的模块有time,datetime和calendar. 1.时钟时间:time 在Python中,用三种方式来表示时间:时间戳,格式化时间字符串和结构化时间. 1)时间戳,就 ...

  4. Python学习总结16:时间模块datetime & time & calendar (三)

    calendar模块 常见函数及说明 1 calendar.calendar(year,w=2,l=1,c=6)   返回一个多行字符串格式的year年年历,3个月一行,间隔距离为c. 每日宽度间隔为 ...

  5. Python学习总结15:时间模块datetime & time & calendar (二)

    二 .datetime模块  1. datetime中常量 1)datetime.MINYEAR,表示datetime所能表示的最小年份,MINYEAR = 1. 2)datetime.MAXYEAR ...

  6. #15 time&datetime&calendar模块

    前言 从这一节开始,记录一些常用的内置模块,模块的学习可能比较无聊,但基础就在这无聊的模块中,话不多说,本节记录和时间相关的模块! 一.time模块 Python中设计时间的模块有很多,但是最常用的就 ...

  7. day18 time、datetime、calendar、sys、os、os.path模块

    今日内容 时间模块 time模块 datetime模块 calendar模块 系统模块 sys模块 os模块 os.path模块 time模块: 在 time 模块中使用最多的方法有: time() ...

  8. 【转载】Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码

    本文转载自脚本之家,源网址为:https://www.jb51.net/article/147429.htm 一.Python中日期时间模块datetime介绍 (一).datetime模块中包含如下 ...

  9. python之模块datetime详解

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块datetime详解 import datetime #data=datetime.dat ...

随机推荐

  1. docker 静默安装mysql

    debconf-set-selections命令 1.功能作用 在debconf database中插入默认值 2.位置 /usr/bin/debconf-set-selections 3.格式用法 ...

  2. 如何利用awk累加第一列的值?

    以下是一个五行文件的例子: 1.[root@master yjt]# seq 5 |awk 'BEGIN{sum=0;print "总和:"}{if(NR<=4)printf ...

  3. [APIO2017]商旅——分数优化+floyd+SPFA判负环+二分答案

    题目链接: [APIO2017]商旅 枚举任意两个点$(s,t)$,求出在$s$买入一个物品并在$t$卖出的最大收益. 新建一条从$s$到$t$的边,边权为最大收益,长度为原图从$s$到$t$的最短路 ...

  4. ICEM-缺角正方体(拓块)

    ​原视频下载地址:https://yunpan.cn/cqFIqQWrNqzNc  访问密码 6fb4

  5. 记录一次腾讯X5内核64位手机初始化失败

    之前一直在使用x5内核,只需要一个jar包和so文件就能让webview实现多余原生webview的水平,在32位的手机上能够正常运行,但是到了64位手机上就报如下错误: E/ERROR:: .... ...

  6. How to transform the day time images to night time ? A series of paper review and some thinkings about this point.

    How to transform the day time images to night time ?  A series of paper review and some thinkings ab ...

  7. word中英文双引号的样式区分与替换技巧

    https://jingyan.baidu.com/article/3f16e003147ea42590c10349.html 场景:一篇word文档中,想要全选更改字体,使得中文全部为“宋体”,英文 ...

  8. 阶段5 3.微服务项目【学成在线】_day09 课程预览 Eureka Feign_09-课程详情页面静态化-静态页面测试

    4 课程详情页面静态化 4.1 静态页面测试 4.1.1 页面内容组成 我们在编写一个页面时需要知道哪些信息是静态信息,哪些信息为动态信息,下图是页面的设计图: 打开静态页面,观察每部分的内容. 红色 ...

  9. QML使用moveToThread线程【QML工程使用C++】

    一.需求来源 对于使用Qt线程,有两种方式,见本人其他文章:https://www.cnblogs.com/judes/p/6884964.html 个人认为QObject::moveToThread ...

  10. 浅谈-对modbus的理解

    浅谈-对modbus的理解 一.简介 Modbus由MODICON公司于1979年开发,是一种工业现场总线协议标准.1996年施耐德公司推出基于以太网TCP/IP的Modbus协议:ModbusTCP ...