目录

Datetime

获取当前时间

获取当前日期

获取当前时间的tuple元组

格式化日期和时间

时间移动

获取两个时间的时间差

时间格式转换

Time

获取距元年(1970.1.1)的秒数

当时时间

格式化时间和日期

休眠

Calendar

打印某个月的月历


Python提供了datetime、time和 calendar 模块用来处理日期和时间,时间间隔是以秒为单位的浮点数。每个时间戳都是自1970年1月1日来算。

Datetime

datetime在python中是用来处理时间的一个模块

datetime模块下又包含了几个类

类名 功能
date 日期对象,常用的属性有year, month, day
time 时间对象
datetime 日期时间对象,常用的属性有hour, minute, second, microsecond
timedelta 时间间隔,即两个时间点之间的长度
datetime_CAPI 日期时间对象C语言接口
tzinfo 时区信息对象

下面介绍datetime中比较常用的一些函数

获取当前时间

from datetime import datetime
>>> print ( datetime.now() )
2018-10-08 08:08:13.296957
>>> print ( datetime.now().time() )
08:08:13.296957
>>> print ( type(datetime.now()) )
<class 'datetime.datetime'>

获取当前日期

from datetime import datetime
>>> print ( datetime.now().date() )
2018-10-08
>>> print ( type(datetime.now().date() ) )
<class 'datetime.date'>

获取当前时间的tuple元组

from datetime import datetime
print(datetime.now().timetuple())
//time.struct_time(tm_year=2018, tm_mon=11, tm_mday=1, tm_hour=16, tm_min=21, tm_sec=5, tm_wday=3, tm_yday=305, tm_isdst=-1)
print(datetime.now().timetuple().tm_year) //2018 年
print(datetime.now().timetuple().tm_mon) //11 月
print(datetime.now().timetuple().tm_mday) //1 日
print(datetime.now().timetuple().tm_hour) //16 时
print(datetime.now().timetuple().tm_min) //21 分
print(datetime.now().timetuple().tm_sec) //5 秒
print(datetime.now().timetuple().tm_wday) //3 0-6 0是周一,6是周日
print(datetime.now().timetuple().tm_yday) //305 一年中的第几天
print(datetime.now().timetuple().tm_isdst) //-1 是否为下令时 1为是 0为不是 -1为未知,默认为-1

格式化日期和时间

from datetime import datetime
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
#############
2018-11-01 19:25:45

时间移动

使用datetime.timedelta这个方法来前后移动时间,可以用的参数有weeks,days,hours,minutes,seconds,microseconds。使用 days=1代表后一天,days=-1代表前一天,其他参数也一样

from datetime import datetime
from datetime import timedelta
>>>print( datetime.now() )
2018-10-08 08:25:49.546137
>>>print( datetime.now()+ timedelta(days=1))
2018-10-09 08:25:49.547196

获取两个时间的时间差

from datetime import datetime
>>>nowtime=datetime.now()
>>>utctime=datetime.utcnow()
>>>print( nowtime-utctime )
8:00:00
>>>print((nowtime-utctime).total_seconds()) ##以秒计算
28800.0

时间格式转换

datetime转str格式

from datetime import datetime
>>>print( datetime.now() )
2018-10-08 08:33:37.369422
>>>print( datetime.now().strftime("%Y-%m-%d %H:%M:%S") )
2018-10-08 08:33:37
>>>print( datetime.now().strftime("%Y-%m-%d") )
2018-10-08
>>>print( datetime.now().strftime("%H-%M:%S") )
08:33:37

str格式转datetime

from datetime import datetime
>>>print( datetime.strptime("2018-10-08 09:00:00","%Y-%m-%d %H:%M:%S"))
2018-10-08 09:00:00
>>>print( type(datetime.strptime("2018-10-08 09:00:00","%Y-%m-%d %H:%M:%S")) )
<class 'datetime.datetime'>

datetime转timestamp

from datetime import datetime
import time
>>>now=datetime.now()
>>>print(now)
2018-10-08 08:51:42.215168
>>>print( time.mktime(now.timetuple()) )
1538959902.0 ##现在距离1970-01-01 00:00:00的秒数

timestamp转datetime

from datetime import datetime
>>>print( datetime.fromtimestamp(1538959902.0))
2018-10-08 08:51:42

Time

获取距元年(1970.1.1)的秒数

>>import time
>>print(time.time())
1541121353.1594238
>>print(time.ctime(time.time()))
Fri Nov 2 09:15:53 2018

当时时间

import time
localtime=time.localtime() #返回结构时间
asctime=time.asctime(localtime) #将结构时间转换为asctime
print(localtime)
print(asctime)
############
time.struct_time(tm_year=2018, tm_mon=11, tm_mday=1, tm_hour=19, tm_min=13, tm_sec=5, tm_wday=3, tm_yday=305, tm_isdst=0)
Thu Nov 1 19:13:05 2018

格式化时间和日期

import time
formattime1=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
formattime2=time.strftime("%a-%b-%d %H:%M:%S:%Y",time.localtime())
print(formattime1)
print(formattime2)
#############################
2018-11-01 19:16:30
Thu-Nov-01 19:16:30:2018

休眠

import time
time.sleep(10) ##休眠10秒

Calendar

Calendar模块有很广泛的方法用来处理年历和月历

打印某一年和某一个月的月历

import calendar

print("2018年的年历是:")
print(calendar.prcal(2018))
###################################
2018年的年历是:
2018 January February March
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 7 1 2 3 4 1 2 3 4
8 9 10 11 12 13 14 5 6 7 8 9 10 11 5 6 7 8 9 10 11
15 16 17 18 19 20 21 12 13 14 15 16 17 18 12 13 14 15 16 17 18
22 23 24 25 26 27 28 19 20 21 22 23 24 25 19 20 21 22 23 24 25
29 30 31 26 27 28 26 27 28 29 30 31 April May June
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 1 2 3 4 5 6 1 2 3
2 3 4 5 6 7 8 7 8 9 10 11 12 13 4 5 6 7 8 9 10
9 10 11 12 13 14 15 14 15 16 17 18 19 20 11 12 13 14 15 16 17
16 17 18 19 20 21 22 21 22 23 24 25 26 27 18 19 20 21 22 23 24
23 24 25 26 27 28 29 28 29 30 31 25 26 27 28 29 30
30 July August September
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 1 2 3 4 5 1 2
2 3 4 5 6 7 8 6 7 8 9 10 11 12 3 4 5 6 7 8 9
9 10 11 12 13 14 15 13 14 15 16 17 18 19 10 11 12 13 14 15 16
16 17 18 19 20 21 22 20 21 22 23 24 25 26 17 18 19 20 21 22 23
23 24 25 26 27 28 29 27 28 29 30 31 24 25 26 27 28 29 30
30 31 October November December
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 7 1 2 3 4 1 2
8 9 10 11 12 13 14 5 6 7 8 9 10 11 3 4 5 6 7 8 9
15 16 17 18 19 20 21 12 13 14 15 16 17 18 10 11 12 13 14 15 16
22 23 24 25 26 27 28 19 20 21 22 23 24 25 17 18 19 20 21 22 23
29 30 31 26 27 28 29 30 24 25 26 27 28 29 30
31 cal=calendar.month(2018,10)
print("2018年10月的月历是:")
print(cal)
#########################
2018年10月的月历是:
October 2018
Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 print(calendar.leapdays(2000,2018)) #返回2000~2018内的闰年数
print(calendar.weekday(2018,11,2)) ##返回2018.11.2是星期几,0-6

Python中的时间日期模块(time、datetime)的更多相关文章

  1. Python中对时间日期的处理方法简单汇总

    这篇文章主要介绍了Python实用日期时间处理方法汇总,本文讲解了获取当前datetime.获取当天date.获取明天/前N天.获取当天开始和结束时间(00:00:00 23:59:59).获取两个d ...

  2. 解决 python 中,时间日期不能序列化的问题

    在python 中, 你在数据库娶到了数据中如果含有时间日期,那么你在向前端作为json对象传递的时候呢,就会报错.大致如下: TypeError: datetime.datetime(2017, 1 ...

  3. python、js 时间日期模块time

    python 参考链接:https://www.runoob.com/python/python-date-time.html 时间戳 >>> print(time.time())# ...

  4. Python中好用的模块们

    目录 Python中好用的模块们 datetime模块 subprocess模块 matplotlib折线图 importlib模块 Python中好用的模块们 datetime模块 ​ 相信我们都使 ...

  5. Python 中的时间处理包datetime和arrow

    Python 中的时间处理包datetime和arrow 在获取贝壳分的时候用到了时间处理函数,想要获取上个月时间包括年.月.日等 # 方法一: today = datetime.date.today ...

  6. Python中xlrd和xlwt模块使用方法 (python对excel文件的操作)

    本文主要介绍可操作excel文件的xlrd.xlwt模块.其中xlrd模块实现对excel文件内容读取,xlwt模块实现对excel文件的写入. 安装xlrd和xlwt模块 xlrd和xlwt模块不是 ...

  7. python中的时间转换

    Python中的时间相关库有: datetime 和time. 主要形式有: datetime timestamp 相互转换: timestamp->datetime: dt = datetim ...

  8. SQLite中的时间日期函数(转)

    SQLite包含了如下时间/日期函数: datetime().......................产生日期和时间date()...........................产生日期tim ...

  9. Python中os和shutil模块实用方法集…

    Python中os和shutil模块实用方法集锦 类型:转载 时间:2014-05-13 这篇文章主要介绍了Python中os和shutil模块实用方法集锦,需要的朋友可以参考下 复制代码代码如下: ...

随机推荐

  1. LeetCode-层数最深叶子结点的和

    层数最深叶子结点的和 LeetCode-1302 这里可以采用上一题中求解二叉树的深度的方法. 因为需要记录最深结点的值的和,所以这里可以边求和,如果遇到不符合最深结点时再将和sum=0. /** * ...

  2. RichTextBox FlowDocument类型操作

      最近研究微信项目,套着web版微信协议做了一个客户端,整体WPF项目MVVM架构及基本代码参考于:http://www.cnblogs.com/ZXdeveloper/archive/2016/1 ...

  3. 从一个想法看 FreeBSD 是商业化还是学院派

    在某知名计算机网络论坛上我看到一个帖子,说自己想根据 FreeBSD 做一个移动的终端操作系统,就像安卓,苹果的 IOS 一样的. 逆向思维当初开发安卓的时候不可能没有考虑过 FreeBSD,因为无论 ...

  4. C语言编程 菜鸟练习100题(21-30)

    [练习21]计算自然数的和 0. 题目: 计算自然数的和 1. 分析: 练习使用 for 循环结构.for 循环允许一个执行指定次数的循环控制结构. 2. 程序: #include <stdio ...

  5. 分形、分形几何、数据可视化、Python绘图

    本系列采用turtle.matplotlib.numpy这三个Python工具,以分形与计算机图像处理的经典算法为实例,通过程序和图像,来帮助读者一步步掌握Python绘图和数据可视化的方法和技巧,并 ...

  6. 远程文件管理系统(SpringBoot + Vue)

    一.简介 可以实现对本地文件的 增.删.改.重命名等操作的监控,通过登录远程文件监控系统,获取一段时间内本地文件的变化情况. 系统功能图如下: 流程图如下: 二.本地文件监控程序的实现(C++) 调用 ...

  7. Flutter 改善套娃地狱问题(仿喜马拉雅PC页面举例)

    前言 这篇文章是我一直以来很想写的一篇文章,终于下定决心动笔了. 写Flutter的小伙伴可能都感受到了:掘金的一些热门的Flutter文章下,知乎的一些Flutter的话题下或者一些论坛里面,喷Fl ...

  8. 一文读懂SuperEdge拓扑算法

    前言 SuperEdge service group 利用 application-grid-wrapper 实现拓扑感知,完成了同一个 nodeunit 内服务的闭环访问 在深入分析 applica ...

  9. 量体裁衣方得最优解:聊聊页面静态化架构和二级CDN建设

    量体裁衣方得最优解:聊聊页面静态化架构和二级CDN建设 上期文章中我们介绍了CDN和云存储的实践,以及云生态的崛起之路,今天,我们继续聊一聊CDN. 我们通常意义上讲的CDN,更多的是针对静态资源类的 ...

  10. Java学习笔记--文件IO

    简介 对于任何程序设计语言,输入和输出(Input\Output)都是系统非常核心的功能,程序运行需要数据,而数据的获取往往需要跟外部系统进行通信,外部系统可能是文件.数据库.其他程序.网络.IO设备 ...