time模块/datetime模块/calendar模块
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模块的更多相关文章
- python日期与日历Datetime和Calendar模块
datetime模块 1.1 概述 datetime比time高级了不少,可以理解为datetime基于time进行了封装,提供了更多的实用的函数,datetime的接口更加的直观,更容易调用 1.2 ...
- Python学习总结14:时间模块datetime & time & calendar (一)
Python中的常用于处理时间主要有3个模块datetime模块.time模块和calendar模块. 一.time模块 1. 在Python中表示时间的方式 1)时间戳(timestamp):通常来 ...
- time,datetime,calendar模块
Python中,与时间有关的模块有time,datetime和calendar. 1.时钟时间:time 在Python中,用三种方式来表示时间:时间戳,格式化时间字符串和结构化时间. 1)时间戳,就 ...
- Python学习总结16:时间模块datetime & time & calendar (三)
calendar模块 常见函数及说明 1 calendar.calendar(year,w=2,l=1,c=6) 返回一个多行字符串格式的year年年历,3个月一行,间隔距离为c. 每日宽度间隔为 ...
- Python学习总结15:时间模块datetime & time & calendar (二)
二 .datetime模块 1. datetime中常量 1)datetime.MINYEAR,表示datetime所能表示的最小年份,MINYEAR = 1. 2)datetime.MAXYEAR ...
- #15 time&datetime&calendar模块
前言 从这一节开始,记录一些常用的内置模块,模块的学习可能比较无聊,但基础就在这无聊的模块中,话不多说,本节记录和时间相关的模块! 一.time模块 Python中设计时间的模块有很多,但是最常用的就 ...
- day18 time、datetime、calendar、sys、os、os.path模块
今日内容 时间模块 time模块 datetime模块 calendar模块 系统模块 sys模块 os模块 os.path模块 time模块: 在 time 模块中使用最多的方法有: time() ...
- 【转载】Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码
本文转载自脚本之家,源网址为:https://www.jb51.net/article/147429.htm 一.Python中日期时间模块datetime介绍 (一).datetime模块中包含如下 ...
- python之模块datetime详解
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块datetime详解 import datetime #data=datetime.dat ...
随机推荐
- [CSP-S模拟测试]:跳房子(模拟)
题目描述 跳房子,是一种世界性的儿童游戏,也是中国民间传统的体育游戏之一.跳房子是在$N$个格子上进行的,$CYJ$对游戏进行了改进,该成了跳棋盘,改进后的游戏是在一个$N$行$M$列的棋盘上进行,并 ...
- Synchronized 有几种用法
我们都知道 Synchronized 是线程安全同步用的,大部分程序可能只会用到同步方法上面.其实 Synchronized 可以用到更多的场合,栈长列举了以下几个用法. 1.同步普通方法 这个也是我 ...
- ubuntu video and audio
推荐你直接安装ubuntu-studio系统.里面有默认安装了很多多媒体软件,主要集中在4个方面1.音频编辑:Jack, Ardour, Audacity, Qtractor. Hydrogen, Y ...
- teraflop级、TFLOPS、TOPS
FLOPS 每秒浮点运算次数,TFLOPS表示每秒万亿(10^12)次浮点计算: TFLOPS是floating point operations per second 每秒所执行的浮点运算次数. 1 ...
- ubuntu系统开机优化参数
date : 2019-06-20 14:34:48 author: headsen chen 临时设置: ulimit -n 1000000 永久设置: vim /etc/security/li ...
- OSError: image file is truncated (28 bytes not processed)
解决办法: 在代码中添加两行 from PIL import ImageFile ImageFile.LOAD_TRUNCATED_IMAGES = True
- pytorch加载数据的方法-没弄,打算弄
参考:https://www.jianshu.com/p/aee6a3d72014 # 网络,netg为生成器,netd为判别器 netg, netd = NetG(opt), NetD(opt) # ...
- ASP程序中调用Now()总显示“上午”和“下午”,如何解决?
ASP程序中调用Now()总显示这样的格式:“2007-4-20 下午 06:06:38”,我要的正确格式为“2007-4-20 18:06:38”,我已经通过控制面板==>区域和语言选项==& ...
- delphi TClientDatset资料
第十一章 TClientDataSet 与TTable.TQuery一样,TClientDataSet也是从TDataSet继承下来的,它通常用于多层体系结构的客户端.TClientDataSet最大 ...
- 使用JavaScript实现量化策略并发执行
本文代码和文章发在FMZ发明者比特币量化交易平台上: 使用JavaScript实现量化策略并发执行--封装Go函数 - 发明者量化 https://www.fmz.com/digest-topic/3 ...