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. class的三元表达式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. ORACLE中声明变量:define variable declare

    在sqlplus 环境中,声明变量的关键字:define variable declare 一.define关键字(host变量) host变量的作用是一个替换作用,是主机环境与oracle进行交互的 ...

  3. Spring StopWatch源码

    import java.text.NumberFormat; import java.util.LinkedList; import java.util.List; public class Stop ...

  4. python 经典排序算法

    python 经典排序算法 排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存.常见的内部排序算 ...

  5. ES6 Syntax and Feature Overview

    View on GitHub Note: A commonly accepted practice is to use const except in cases of loops and reass ...

  6. dapper 异步修改方法

    public async Task<int> UpdateStatusByWarehouseId(bool isValid, int warehouseId) { using (var c ...

  7. iOS 判断scrollView是否滑动到底部

      判断scrollView有没有滚动到视图的底部,用来判断下拉刷新的时间.等 - (void)scrollViewDidScroll:(UIScrollView *)scrollView1 { CG ...

  8. Linux自动获取IP地址 重启不会消失

    网络设置 root用户登录    编辑 /etc/sysconfig/network-scripts/ifcfg-eth0 将ONBOOT=no 改为 ONBOOT=yes 并保存 重启network ...

  9. PAT 甲级 1033 To Fill or Not to Fill (25 分)(贪心,误以为动态规划,忽视了油量问题)*

    1033 To Fill or Not to Fill (25 分)   With highways available, driving a car from Hangzhou to any oth ...

  10. (四)HttpServletRequest对象(转)

    转自“孤傲苍狼”博客. Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象. request和response对象即 ...