python 时间模块备忘

简介:

  由于要用到时间模块,为了下次不用去翻文档,这里也简单记录一下:

直接一个脚本看输出:

1
2
3
4
5
6
7
8
9
import  time
print  time.time()
print  time.localtime(time.time())
print  time.strftime( '%Y-%m-%d' , time.localtime())
print  time.strftime( '%y-%m-%d' , time.localtime())
print  time.strftime( '%Y-%m-%d %H:%M:%S' , time.localtime())
print  time.strftime( '%Y-%m-%d %I:%M:%S' , time.localtime())
print  time.strftime( '%Y-%m-%d %H:%M:%S --%A--%c' , time.localtime())
print  time.strftime( '%Y-%m-%d %H:%M:%S --%x--%X' , time.localtime())

查看结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@www python] # python time1.py 
1425623399.84
time.struct_time(tm_year = 2015 , tm_mon = 3 , tm_mday = 6 , tm_hour = 14 , tm_min = 29 , tm_sec = 59 , tm_wday = 4 , tm_yday = 65 , tm_isdst = 0 )
2015 - 03 - 06
15 - 03 - 06
2015 - 03 - 06  14 : 29 : 59
2015 - 03 - 06  02 : 29 : 59
2015 - 03 - 06  14 : 29 : 59  - - Friday - - Fri Mar   6  14 : 29 : 59  2015
2015 - 03 - 06  14 : 29 : 59  - - 03 / 06 / 15 - - 14 : 29 : 59
 
 
datetime模块定义了下面这几个类:
datetime.date:表示日期的类。常用的属性有year, month, day;
datetime.time:表示时间的类。常用的属性有hour, minute, second, microsecond;
datetime.datetime:表示日期时间。
datetime.timedelta:表示时间间隔,即两个时间点之间的长度。
 
 
>>>  from  datetime  import  *
>>>  print  datetime.today()
2015 - 03 - 06  14 : 43 : 46.870936
>>>  print  datetime.now()  
2015 - 03 - 06  14 : 43 : 51.313098


需求:查看100天前是几月几号:

1
2
import  datetime
(datetime.datetime.now()  -  datetime.timedelta(days  =  100 )).strftime( "%Y-%m-%d" )








本文转自 小罗ge11 51CTO博客,原文链接:http://blog.51cto.com/xiaoluoge/1617932,如需转载请自行联系原作者
目录
相关文章
|
24天前
|
存储 开发者 Python
Python中的collections模块与UserDict:用户自定义字典详解
【4月更文挑战第2天】在Python中,`collections.UserDict`是用于创建自定义字典行为的基类,它提供了一个可扩展的接口。通过继承`UserDict`,可以轻松添加或修改字典功能,如在`__init__`和`__setitem__`等方法中插入自定义逻辑。使用`UserDict`有助于保持代码可读性和可维护性,而不是直接继承内置的`dict`。例如,可以创建一个`LoggingDict`类,在设置键值对时记录操作。这样,开发者可以根据具体需求定制字典行为,同时保持对字典内部管理的抽象。
|
26天前
|
存储 缓存 算法
Python中collections模块的deque双端队列:深入解析与应用
在Python的`collections`模块中,`deque`(双端队列)是一个线程安全、快速添加和删除元素的双端队列数据类型。它支持从队列的两端添加和弹出元素,提供了比列表更高的效率,特别是在处理大型数据集时。本文将详细解析`deque`的原理、使用方法以及它在各种场景中的应用。
|
3天前
|
开发者 Python
Python的os模块详解
Python的os模块详解
15 0
|
6天前
|
数据挖掘 API 数据安全/隐私保护
python请求模块requests如何添加代理ip
python请求模块requests如何添加代理ip
|
8天前
|
测试技术 Python
Python 有趣的模块之pynupt——通过pynput控制鼠标和键盘
Python 有趣的模块之pynupt——通过pynput控制鼠标和键盘
|
8天前
|
Serverless 开发者 Python
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
40 1
|
10天前
|
Python
python学习-函数模块,数据结构,字符串和列表(下)
python学习-函数模块,数据结构,字符串和列表
52 0
|
11天前
|
Python
python学习14-模块与包
python学习14-模块与包
|
13天前
|
SQL 关系型数据库 数据库
Python中SQLite数据库操作详解:利用sqlite3模块
【4月更文挑战第13天】在Python编程中,SQLite数据库是一个轻量级的关系型数据库管理系统,它包含在一个单一的文件内,不需要一个单独的服务器进程或操作系统级别的配置。由于其简单易用和高效性,SQLite经常作为应用程序的本地数据库解决方案。Python的内置sqlite3模块提供了与SQLite数据库交互的接口,使得在Python中操作SQLite数据库变得非常容易。
|
18天前
|
索引 Python
「Python系列」Python operator模块、math模块
Python的`operator`模块提供了一系列内置的操作符函数,这些函数对应于Python语言中的内建操作符。使用`operator`模块可以使代码更加清晰和易读,同时也能提高性能,因为它通常比使用Python内建操作符更快。
27 0