Python 学习笔记 - 时间模块

简介:

Python里面用来管理时间的模块有2个,分别是time模块和datetime模块,现在看看如何使用


首先看看time模块

例1

1
2
3
4
import  time
print (time.time())  #时间戳,1970年到现在的秒数
- - - - - - - - - - - - - - - - - - -
1474347039.991068

例2

1
2
3
print (time.ctime())  #当前系统时间字符串格式
- - - - - - - - - - - - - - - - - - -
Tue Sep  20  14 : 50 : 39  2016

例3

1
2
3
print (time.ctime(time.time() - 86400 ))  #根据时间戳算时间
- - - - - - - - - - - - - - - - - - - - -
Mon Sep  19  14 : 50 : 39  2016

例4

1
2
3
4
5
6
7
8
#显示的是格林威治时间
print (time.gmtime())
time_obj = time.gmtime()
print (time_obj.tm_year,time_obj.tm_mon)
 
- - - - - - - - - - - - - - - - - - - - -
time.struct_time(tm_year = 2016 , tm_mon = 9 , tm_mday = 20 , tm_hour = 4 , tm_min = 50 , tm_sec = 39 , tm_wday = 1 , tm_yday = 264 , tm_isdst = 0 )
2016  9


例5

1
2
3
4
#显示本地时间
print (time.localtime())
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
time.struct_time(tm_year = 2016 , tm_mon = 9 , tm_mday = 20 , tm_hour = 14 , tm_min = 50 , tm_sec = 39 , tm_wday = 1 , tm_yday = 264 , tm_isdst = 0 )

例6

1
2
3
4
#必须传入一个时间对象的参数,把structure time转换成时间戳
print (time.mktime(time_obj))
- - - - - - - - - - - - - - - - - - - - -
1474311039.0

例7

1
2
#延时多少秒
time.sleep( 4 )

例8

1
2
3
4
#把时间对象转成字符串格式
print (time.strftime( "%Y-%m-%d %H:%M:%S" ,time_obj))
- - - - - - - - - - - - - - - - - - - -
2016 - 09 - 20  04 : 50 : 39

例9


1
2
3
4
5
6
7
#把字符串格式转换为时间对象
tm = time.strptime( "2016-05-10 15:04:20" , "%Y-%m-%d %H:%M:%S" )
print (tm)
print (time.mktime(tm))
- - - - - - - - - - - - - - - - - - - - -
time.struct_time(tm_year = 2016 , tm_mon = 5 , tm_mday = 10 , tm_hour = 15 , tm_min = 4 , tm_sec = 20 , tm_wday = 1 , tm_yday = 131 , tm_isdst = - 1 )
1462856660.0



接下来看看datetime模块


例1

1
2
3
4
import   datetime
print (datetime.date.today()) #输出当前日期
- - - - - - - - - - - -
2016 - 09 - 20

例2

1
2
3
4
currenttime = datetime.datetime.now() #输出当前时间,最常用
print (currenttime)
- - - - - - - - - - - -
2016 - 09 - 20  14 : 50 : 44.018886

例3

1
2
3
4
#比当前时间加10天
new_date = datetime.date.today() + datetime.timedelta(days = 10 )
print (new_date)
2016 - 09 - 30

例4

1
2
3
4
5
#比当前时间少1个小时
new_date = datetime.datetime.now() + datetime.timedelta(hours = - 1 )
print (new_date)
- - - - - - - - - - - -
2016 - 09 - 20  13 : 50 : 44.018923

例5

#直接替换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
print (currenttime.replace( 2014 , 9 , 12 ))
- - - - - - - - -
2014 - 09 - 12  14 : 50 : 44.018886
 
print (currenttime.replace(year = 2015 ))
- - - - - - - - -
2015 - 09 - 20  14 : 50 : 44.018886
 
time_obj = currenttime.replace( 2015 )
print (time_obj, type (time_obj))
- - - - - - - - - - -
2015 - 09 - 20  14 : 50 : 44.018886  < class  'datetime.datetime' >
 
print (currenttime>time_obj)
- - - - - - - - - - -
True









本文转自 beanxyz 51CTO博客,原文链接:http://blog.51cto.com/beanxyz/1854443,如需转载请自行联系原作者

目录
相关文章
|
1天前
|
JSON JavaScript 数据格式
|
1天前
|
监控 Python
paramiko 模块 ---Python脚本监控当前系统的CPU、内存、根目录、IP地址等信息
paramiko 模块 ---Python脚本监控当前系统的CPU、内存、根目录、IP地址等信息
|
11天前
|
XML 数据格式 Python
Python的`import`用于加载模块,基础形式是`import module`,全量导入
【6月更文挑战第23天】Python的`import`用于加载模块,基础形式是`import module`,全量导入;`from module import name`选择性导入部分,减少命名空间污染;`from module import *`导入所有(不推荐),易引发冲突。别名导入如`from math import sqrt as square_root`可避免冲突。包导入用`.`,如`import xml.etree.ElementTree as ET`。
35 8
|
11天前
|
XML 数据格式 Python
在Python中,导入其他模块是通过使用import语句完成的
在Python中导入模块涉及`import`语句的不同用法:1) `import math`导入整个标准库;2) `from math import sqrt`导入单个函数;3) `import numpy as np`使用别名;4) `from random import *`导入所有(不推荐);5) `import xml.etree.ElementTree as ET`导入子模块;6) 使用`importlib.import_module()`延迟导入;7) `from .module import func`导入相对路径模块,需管理`sys.path`。
30 6
|
8天前
|
Python
python--os模块
python--os模块
11 2
|
8天前
|
Python
python-time模块
python-time模块
9 1
|
10天前
|
算法 数据安全/隐私保护 开发者
Python logger模块详细讲解
Python logger模块详细讲解
15 3
|
11天前
|
Python
Python的`os`模块核心功能概述:通过`os.getcwd()`获取
【6月更文挑战第23天】Python的`os`模块核心功能概述:通过`os.getcwd()`获取、`os.chdir()`改变工作目录;使用`os.mkdir()`, `os.makedirs()`创建目录,`os.rmdir()`, `os.removedirs()`删除;`os.rename()`, `os.renames()`重命名文件或目录;`os.remove()`删除文件;`os.listdir()`列出目录内容;`os.path.exists()`, `os.path.isfile()`, `os.path.isdir()`检查路径;`os.stat()`获取文件属性。
20 4
|
11天前
|
XML 数据格式 Python
Python模块导入包括:`import math`导入标准库
【6月更文挑战第23天】Python模块导入包括:`import math`导入标准库,`from math import sqrt`导入单个函数,`import numpy as np`给模块取别名,`from random import *`导入所有(不推荐),`import xml.etree.ElementTree as ET`导入子模块,`import_module(&#39;pandas&#39;)`按需导入,和使用相对路径如`from .module import func`处理项目结构。记得调整`sys.path`以包含自定义模块路径。
21 4
|
10天前
|
Linux Python
Python- jinja2模块详解
Python- jinja2模块详解
11 1