[原] Python日期/时间操作方法使用
2009-06-03 14:18
549 查看
Python日期/时间操作方法使用
0. 模块:
import os, sys
import time, datetime
1. 得到当前时间
(1) Based on time module:
import os, sys, time, datetime
startTime = time.localtime()
注意这里: startTime
<type 'time.struct_time'>
(2) Based on datetime module:
import os, sys, time, datetime
nowTime = datetime.datetime.now()
type(nowTime)
<type 'datetime.datetime'>
2. 日期转字符串:
(1) str(xx)
(2) time提供的函数:
time.strftime([format], time)
strStartTime = time.strftime('%Y-%m-%d %H:%M:%S', startTime)
'2009-06-03 13:44:51'
3. 字符串转日期:
(1) 字符串转time:
d = time.strptime(strStartTime, '%Y-%m-%d %H:%M:%S')
(2009, 6, 3, 13, 44, 51, 2, 154, -1)
type(d)
<type 'time.struct_time'>
(2) 字符串转datetime
datetime的好处是可以实现方便的时间运算,比如 endTime - starTime,这在时间duration计算时非常方便.
# Convert string start time and end time to datetime.datetime
startTime = datetime.datetime(tmpStartTime[0], tmpStartTime[1], tmpStartTime[2], tmpStartTime[3], tmpStartTime[4], tmpStartTime[5] );
基于上面的转换.
0. 模块:
import os, sys
import time, datetime
1. 得到当前时间
(1) Based on time module:
import os, sys, time, datetime
startTime = time.localtime()
注意这里: startTime
<type 'time.struct_time'>
(2) Based on datetime module:
import os, sys, time, datetime
nowTime = datetime.datetime.now()
type(nowTime)
<type 'datetime.datetime'>
2. 日期转字符串:
(1) str(xx)
(2) time提供的函数:
time.strftime([format], time)
strStartTime = time.strftime('%Y-%m-%d %H:%M:%S', startTime)
'2009-06-03 13:44:51'
3. 字符串转日期:
(1) 字符串转time:
d = time.strptime(strStartTime, '%Y-%m-%d %H:%M:%S')
(2009, 6, 3, 13, 44, 51, 2, 154, -1)
type(d)
<type 'time.struct_time'>
(2) 字符串转datetime
datetime的好处是可以实现方便的时间运算,比如 endTime - starTime,这在时间duration计算时非常方便.
# Convert string start time and end time to datetime.datetime
startTime = datetime.datetime(tmpStartTime[0], tmpStartTime[1], tmpStartTime[2], tmpStartTime[3], tmpStartTime[4], tmpStartTime[5] );
基于上面的转换.
相关文章推荐
- Python日期/时间操作方法使用
- Python 日期,时间和函数的使用
- python 使用time 进行时间、时间戳、日期格式转换
- 在Python中操作日期和时间之gmtime()方法的使用
- python日期时间使用总结
- Python Flask Web 第七课 —— 使用 flask-moment 本地化日期和时间
- 在Python的Flask框架中使用日期和时间的教程
- python 使用datetime进行时间日期转换
- 在Python中操作日期和时间之gmtime()方法的使用
- 在Python操作时间和日期之asctime()方法的使用
- python 使用datetime模块timedelta实现日期时间相加
- python 使用datetime模块timedelta实现日期时间相加
- 在Python操作时间和日期之asctime()方法的使用
- delphi API使用例子,日期时间等
- php处理excel时间数据--——使用PHPExcel判别和格式化Excel中的日期格式
- python_时间日期
- Python 日期和时间
- Python 日期和时间
- python,php 日期时间和时间戳相互转化
- 在Python中操作时间之tzset()方法的使用教程