python时间处理模块 datetime time模块 deltetime模块

简介:

1 首先介绍time模块,因为简单

python 自带模块

本人使用time模块,只使用两个函数

time函数和sleep函数


import time

a.     time.time()   函数 返回unix时间  常用作两个时间差的计算


b.     time.sleep()  休眠多久,精度为子秒(subsecond)

 

1
2
3
4
5
6
7
In [ 90 ]: t1  =  time.time()
In [ 91 ]: t1
Out[ 91 ]:  1461400225.877932
In [ 92 ]: time.sleep( 2 )
In [ 93 ]: t2  =  time.time()
In [ 94 ]: t2  -  t1
Out[ 94 ]:  24.2673659324646



2 其他处理时间的场景,本人都是使用datetime模块

python 自带

from datetime import datetime

常用时间场景 #获取当前时间

1
2
In [ 95 ]:  from  datetime  import  datetime
In [ 96 ]: now  =  datetime.now()


#注意类型,时间之间的计算一定要是时间类型,即相同类型之间可以加减

1
2
3
4
In [ 97 ]:  type (now)     
Out[ 97 ]: datetime.datetime
In [ 98 ]: now
Out[ 98 ]: datetime.datetime( 2016 ,  4 ,  23 ,  16 ,  33 ,  0 ,  143269 )




a.     常用获取 当前的年份,月份,日期,时,周(注意周的数值,0-6  0表示周一,依次类推)


1
2
3
In [ 116 ]: year, month, today, hour, week  =  (now.year, now.month, now.day, now.hour, now.weekday())
In [ 117 ]: year, month, today, hour, week
Out[ 117 ]: ( 2016 ,  4 ,  23 ,  16 ,  5 )


b.    ctime()函数  返回可读的字符串格式

1
2
In [ 118 ]: now.ctime()
Out[ 118 ]:  'Sat Apr 23 16:33:00 2016'


c.    strftime() 函数 将datetime时间格式,转换成为指定的字符串格式

1
2
In [ 121 ]: now.strftime( '%Y-%m-%d' )
Out[ 121 ]:  '2016-04-23'


     返回昨天的unix时间    

1
2
3
now =  datetime.now()
yesterday  =  now  -  timedelta( 1 )
yest_clock  =  (datetime.now()   - timedelta( 1 )).strftime( '%s' )


d.    strptime() 函数,将字符串模式的时间,转化成为datetime格式

1
2
In [ 123 ]: datetime.strptime( '20160101' , '%Y%m%d' )
Out[ 123 ]: datetime.datetime( 2016 ,  1 ,  1 ,  0 ,  0 )



3 timedelta

来自datetime模块

from datetime import timedelta


作用计算datetime类型时间的一个差值(得到或将来的一个)


常用获取前一天的datetime时间

1
2
3
4
In [ 129 ]: now
Out[ 129 ]: datetime.datetime( 2016 ,  4 ,  23 ,  16 ,  33 ,  0 ,  143269 )
In [ 130 ]: now  -  timedelta( 1 )
Out[ 130 ]: datetime.datetime( 2016 ,  4 ,  22 ,  16 ,  33 ,  0 ,  143269 )


获取一个小时之前的时间

1
2
In [ 134 ]: now
Out[ 134 ]: datetime.datetime( 2016 ,  4 ,  23 ,  16 ,  33 ,  0 ,  143269 )



一秒前,一小时前,一天前,一分中之前

但是没有一年前,一个月前勒(这个原因你能想明白的)

1
2
3
4
5
6
7
8
9
10
In [ 158 ]: now  -  timedelta(seconds = 1 )
Out[ 158 ]: datetime.datetime( 2016 ,  4 ,  23 ,  16 ,  32 ,  59 ,  143269 )
In [ 159 ]: now  -  timedelta(seconds = 1 )
Out[ 159 ]: datetime.datetime( 2016 ,  4 ,  23 ,  16 ,  32 ,  59 ,  143269 )
In [ 160 ]: now  -  timedelta(hours = 1 )
Out[ 160 ]: datetime.datetime( 2016 ,  4 ,  23 ,  15 ,  33 ,  0 ,  143269 )
In [ 161 ]: now  -  timedelta(days = 1 )
Out[ 161 ]: datetime.datetime( 2016 ,  4 ,  22 ,  16 ,  33 ,  0 ,  143269 )
In [ 162 ]: now  -  timedelta(minutes = 1 )
Out[ 162 ]: datetime.datetime( 2016 ,  4 ,  23 ,  16 ,  32 ,  0 ,  143269 )



获取上周 


1
2
3
4
5
6
7
8
9
In [ 4 ]:  from  datetime  import  datetime
In [ 5 ]: now  =  datetime.now()         
In [ 6 ]:  from  datetime  import  datetime,  timedelta
In [ 7 ]: now  =  datetime.now()                     
In [ 8 ]: last_week  =  now  - timedelta(weeks = 1 )
In [ 9 ]: now
Out[ 9 ]: datetime.datetime( 2016 ,  5 ,  4 ,  21 ,  39 ,  35 ,  424140 )
In [ 10 ]: last_week
Out[ 10 ]: datetime.datetime( 2016 ,  4 ,  27 ,  21 ,  39 ,  35 ,  424140 )


1
2
In [ 12 ]: now.weekday()
Out[ 12 ]:  2


注意:

星期n python中代表
一 0
二 1
三 2
四 3
五 4
六 5
日 6
本文转自残剑博客51CTO博客,原文链接http://blog.51cto.com/cuidehua/1767046如需转载请自行联系原作者


cuizhiliang

相关文章
|
SQL 关系型数据库 数据库
Python SQLAlchemy模块:从入门到实战的数据库操作指南
免费提供Python+PyCharm编程环境,结合SQLAlchemy ORM框架详解数据库开发。涵盖连接配置、模型定义、CRUD操作、事务控制及Alembic迁移工具,以电商订单系统为例,深入讲解高并发场景下的性能优化与最佳实践,助你高效构建数据驱动应用。
1473 7
|
监控 安全 程序员
Python日志模块配置:从print到logging的优雅升级指南
从 `print` 到 `logging` 是 Python 开发的必经之路。`print` 调试简单却难维护,日志混乱、无法分级、缺乏上下文;而 `logging` 支持级别控制、多输出、结构化记录,助力项目可维护性升级。本文详解痛点、优势、迁移方案与最佳实践,助你构建专业日志系统,让程序“有记忆”。
1209 0
|
JSON 算法 API
Python中的json模块:从基础到进阶的实用指南
本文深入解析Python内置json模块的使用,涵盖序列化与反序列化核心函数、参数配置、中文处理、自定义对象转换及异常处理,并介绍性能优化与第三方库扩展,助你高效实现JSON数据交互。(238字)
1010 4
|
安全 大数据 程序员
Python operator模块的methodcaller:一行代码搞定对象方法调用的黑科技
`operator.methodcaller`是Python中处理对象方法调用的高效工具,替代冗长Lambda,提升代码可读性与性能。适用于数据过滤、排序、转换等场景,支持参数传递与链式调用,是函数式编程的隐藏利器。
391 4
|
Java 调度 数据库
Python threading模块:多线程编程的实战指南
本文深入讲解Python多线程编程,涵盖threading模块的核心用法:线程创建、生命周期、同步机制(锁、信号量、条件变量)、线程通信(队列)、守护线程与线程池应用。结合实战案例,如多线程下载器,帮助开发者提升程序并发性能,适用于I/O密集型任务处理。
1152 0
|
XML JSON 数据处理
超越JSON:Python结构化数据处理模块全解析
本文深入解析Python中12个核心数据处理模块,涵盖csv、pandas、pickle、shelve、struct、configparser、xml、numpy、array、sqlite3和msgpack,覆盖表格处理、序列化、配置管理、科学计算等六大场景,结合真实案例与决策树,助你高效应对各类数据挑战。(238字)
1584 0
|
存储 数据库 开发者
Python SQLite模块:轻量级数据库的实战指南
本文深入讲解Python内置sqlite3模块的实战应用,涵盖数据库连接、CRUD操作、事务管理、性能优化及高级特性,结合完整案例,助你快速掌握SQLite在小型项目中的高效使用,是Python开发者必备的轻量级数据库指南。
1134 0
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
1994 102
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
706 104
|
人工智能 自然语言处理 算法框架/工具
Python:现代编程的首选语言
Python:现代编程的首选语言
562 103

推荐镜像

更多