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

相关文章
|
5天前
|
JSON 数据格式 Python
Python标准库中包含了json模块,可以帮助你轻松处理JSON数据
【4月更文挑战第30天】Python的json模块简化了JSON数据与Python对象之间的转换。使用`json.dumps()`可将字典转为JSON字符串,如`{"name": "John", "age": 30, "city": "New York"}`,而`json.loads()`则能将JSON字符串转回字典。通过`json.load()`从文件读取JSON数据,`json.dump()`则用于将数据写入文件。
11 1
|
6天前
|
Python 容器
python内置函数、数学模块、随机模块(二)
python内置函数、数学模块、随机模块(二)
|
6天前
|
索引 Python
python内置函数、数学模块、随机模块(一)
python内置函数、数学模块、随机模块(一)
|
9天前
|
人工智能 安全 Java
Python 多线程编程实战:threading 模块的最佳实践
Python 多线程编程实战:threading 模块的最佳实践
125 5
|
9天前
|
人工智能 数据库 开发者
Python中的atexit模块:优雅地处理程序退出
Python中的atexit模块:优雅地处理程序退出
9 3
|
12天前
|
存储 开发者 Python
Python中的argparse模块:命令行参数解析的利器
Python中的argparse模块:命令行参数解析的利器
16 2
|
12天前
|
开发者 Python
Python的os模块详解
Python的os模块详解
17 0
|
15天前
|
数据挖掘 API 数据安全/隐私保护
python请求模块requests如何添加代理ip
python请求模块requests如何添加代理ip
|
16天前
|
测试技术 Python
Python 有趣的模块之pynupt——通过pynput控制鼠标和键盘
Python 有趣的模块之pynupt——通过pynput控制鼠标和键盘
|
16天前
|
Serverless 开发者 Python
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
42 1