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. c++ 读取文件字符串 并且解析

    /* "/Users/macname/Desktop/aa-1.log" 链接:https://pan.baidu.com/s/1fKB5vXDe6bYOhoslc-kr7w  密 ...

  2. 浅谈Min_25筛(一看就懂的那种)

    作用 前提:一个积性函数\(F(i)\),要求\(F(P^k),P\in prime\)可以快速计算 实现\(O(\frac{n^{\frac{3}{4}}}{logn})\):\(\sum\limi ...

  3. 1、Kfaka 部署

    一.使用Kfaka自带的zookeeper服务. 1.下载Kafka,下载地址: http://kafka.apache.org/downloads 2.区分Kfaka版本: Scala 2.12 - ...

  4. 【2018.08.01】(表/栈/队列/大小顶堆)学习Stark和Queue算法小记

    Train Problem I As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...

  5. 深入了解JVM虚拟机8:Java的编译期优化与运行期优化

    java编译期优化 java语言的编译期其实是一段不确定的操作过程,因为它可以分为三类编译过程:1.前端编译:把.java文件转变为.class文件2.后端编译:把字节码转变为机器码3.静态提前编译: ...

  6. DNN在推荐系统中的应用参考资料

    参考资料 DSSM算法计算文本相似度:https://www.cnblogs.com/wmx24/p/10157154.html Deep Neural Network for YouTube Rec ...

  7. bash常用的快捷键

    bash常用快捷键 快捷键 作用 CTRL+A 把光标移动到命令行开头.如果我们输入的命令过长,则在想要把光标移动到命令行开头时使用 CTRL+E 光标移动到命令行行尾 CTRL+C 强制中止当前命令 ...

  8. Colab使用教程

    目录 有关链接 使用GPU 切换文件夹 参考 有关链接 Google Colabratory Google Drive 使用GPU 以下两种方式都可以: "修改"->&quo ...

  9. qt 创建程序目录

    voidinitDir() { QStringuserFilePath=QStandardPaths::writableLocation(QStandardPaths::DocumentsLocati ...

  10. Flutter移动电商实战 --(48)详细页_详情和评论的切换

    增加切换的效果,我们主要是修改这个地方 这样我们的评论的内容就显示出来了 最终代码 details_web.dart import 'package:flutter/material.dart'; i ...