9Python标准库系列之time模块

Python标准库系列之time模块

This module provides various functions to manipulate time values.


方法名 说明
time.sleep(int) 等待时间
time.time() 输出时间戳,从1970年1月1号到现在用了多少秒
time.ctime() 返回当前的系统时间
time.gmtime() 将时间戳转换成struct_time格式
time.localtime() 以struct_time格式返回本地时间
time.mktime(time.localtime()) 将struct_time格式转回成时间戳格式
time.strftime(“%Y-%m-%d %H:%M:%S”,time.gmtime()) 将struct_time格式转成指定的字符串格式
time.strptime(“2016-01-28”,”%Y-%m-%d”) 将字符串格式转换成struct_time格式

查看当前时间

>>> time.time()
# 以时间戳的形式返回
1464154805.82723
>>> time.ctime()
‘Wed May 25 13:42:51 2016‘

返回当前的昨天时间

>>> time.ctime()
‘Wed May 25 13:42:51 2016‘
# 今天的时间减去86640秒
>>> time.ctime(time.time()-86640)
‘Tue May 24 13:39:58 2016‘

将时间戳转换成struct_time格式

>>> time.gmtime(time.time()-86640)
time.struct\_time(tm\_year=2016, tm\_mon=5, tm\_mday=24, tm\_hour=5, tm\_min=42, tm\_sec=23, tm\_wday=1, tm\_yday=145, tm_isdst=0)
>>> obj = time.gmtime(time.time()-86640)
>>> obj.tm_year
2016
>>> obj.tm_mon
5
>>> "%s-%s-%s" % (obj.tm\_year, obj.tm\_mon, obj.tm_mday)
‘2016-5-24‘

格式化

  1. >>> import time
    >>> t = time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time()))
    >>> t
    ‘2016-09-21 14:04:54‘

#Python标准库 #Time

时间: 2024-08-20 18:30:36

9Python标准库系列之time模块的相关文章

22Python标准库系列之Redis模块

Python标准库系列之Redis模块 What is redis? Redis is an open source (BSD licensed), in-memory data structure store, used as database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, b

11Python标准库系列之configparser模块

Python标准库系列之configparser模块 This module provides the ConfigParser class which implements a basic configuration language which provides a structure similar to what's found in Microsoft Windows INI files. You can use this to write Python programs which

4Python标准库系列之sys模块

Python标准库系列之sys模块 This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It is always available. sys模块用于提供对解释器相关的操作 模块方法 解释说明 sys.argv 传递到Python脚本的命令行参数列表,第一个元

12Python标准库系列之subprocess模块

Python标准库系列之subprocess模块 This module allows you to spawn processes, connect to their input/output/error pipes, and obtain their return codes. 常用方法实例 call() 执行命令,并返回状态码,状态码0代表命令执行成功,其他的都表示命令执行不成功 >>> ret = subprocess.call(["ls", "-l

7Python标准库系列之requests模块

Python标准库系列之requests模块 Requests is the only Non-GMO HTTP library for Python, safe for human consumption. 官方文档:http://docs.python-requests.org/en/master/ 安装Requests模块 Requests模块官方提供了两种方式安装: pip方式安装 pip install requests 源码方式安装 git clone git://github.co

5Python标准库系列之json模块

Python标准库系列之json模块 JSON (JavaScript Object Notation) http://json.org is a subset of JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data interchange format. JSON通常用于在Web客户端和服务器数据交换,即把字符串类型的数据转换成Python基本数据类型或者将Python基本数据类型转换成字符串类型. 常用方法

13Python标准库系列之shutil模块

Python标准库系列之shutil模块 The shutil module offers a number of high-level operations on files and collections of files. In particular, functions are provided which support file copying and removal. For operations on individual files, see also the os modul

14Python标准库系列之zipfile模块

Python标准库系列之zipfile模块 The ZIP file format is a common archive and compression standard. This module provides tools to create, read, write, append, and list a ZIP file. This module does not currently handle multi-disk ZIP files. It can handle ZIP file

10Python全栈之路系列之深浅拷贝标准库系列之datetime模块

Python标准库系列之datetime模块 Fast implementation of the datetime type. 功能 说明 datetime.date.today() 打印输出当前的系统日期 datetime.date.fromtimestamp(time.time()) 将时间戳转成日期格式 datetime.datetime.now() 打印当前的系统时间 current_time.replace(2016,5,12) 返回当前时间,但指定的值将被替换 datetime.d