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 ...
随机推荐
- python pillow 处理图片
demo1 #打开图片,并随机添加一些椒盐噪声 from PIL import Image import numpy as np import matplotlib.pyplot as plt img ...
- 小程序 之嵌套循环修改index与item
<block wx:for="{{menu}}" wx:key="index"> <view class="cate-item {{ ...
- RethinkDB是什么?—— 面向文档的NOSQL数据库,MVCC+Btree索引,pushes JSON to your apps in realtime采用push思路,优化的ssd存储
RethinkDB是什么? RethinkDB是新一代的面向文档的数据库存储管理系统,原本是MySQL中针对SSD优化的一个存储引擎,后来脱离了MySQL成为了独立的系统. 数据如何存储在磁盘上? 数 ...
- CentOS 7 安装MySQL8
安装 下载Linux对应的MySQL https://dev.mysql.com/downloads/mysql/ CentOS选择Red Hat Enterprise Linux 在/usr/loc ...
- legend3---17、如何抽象和复用控制器中的方法
legend3---17.如何抽象和复用控制器中的方法 一.总结 一句话总结: 比如不同的控制器中都用了检查手机号是否已经注册,是否没注册这样的功能,我应该如何抽象和复用方法 新建一个处理手机号码的模 ...
- Razor syntax reference for ASP.NET Core
Razor syntax reference for ASP.NET Core Razor is a markup syntax for embedding server-based code int ...
- 解决Mac OS编译安装时出现 cannot find openssl's <evp.h> 错误的问题
踩坑 最近通过pecl安装mongodb扩展时,提示以下错误 ...... configure: error: Cannot find OpenSSL's <evp.h> ...... 根 ...
- 23 Flutter官方推荐的状态管理库provider的使用
加群452892873 下载对应21可文件,运行方法,建好项目,直接替换lib目录,在往pubspec.yaml添加上一下扩展. cupertino_icons: ^ flutter_swiper: ...
- React调试——visual studio code
原文链接:Using React in Visual Studio Code 原文链接:Live edit and debug your React apps directly from VS Cod ...
- myeclipse打开jsp后卡死的问题详解
myeclipse打开jsp后卡死的问题详解 1,打开 Window -> Preferences -> General -> Editors -> File Associ ...