time模块,datetime模块
2024-09-30 13:13:39
time模块
time模块是包含各方面对时间操作的函数. 尽管这些常常有效但不是所有方法在任意平台中有效。
时间相关的操作,时间有三种表示方式:
- 时间戳 1970年1月1日之后的秒,即:time.time()
- 格式化的字符串 2014-11-11 11:11, 即:time.strftime('%Y-%m-%d')
- 结构化时间 元组包含了:年、日、星期等... time.struct_time 即: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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
import time # ========== 时间戳 ========== print time.time() print time.mktime(time.localtime()) # ========== 元组形式 ========== print time.gmtime() #可加时间戳参数 print time.localtime() #可加时间戳参数 print time.strptime( '2014-11-11' , '%Y-%m-%d' ) # ========== 字符串形式 ========== print time.strftime( '%Y-%m-%d' ) #默认当前时间 print time.strftime( '%Y-%m-%d' ,time.localtime()) #默认当前时间 print time.asctime() print time.asctime(time.localtime()) print time.ctime(time.time()) """ 格式化占位符: %Y Year with century as a decimal number. %m Month as a decimal number [01,12]. %d Day of the month as a decimal number [01,31]. %H Hour (24-hour clock) as a decimal number [00,23]. %M Minute as a decimal number [00,59]. %S Second as a decimal number [00,61]. %z Time zone offset from UTC. %a Locale's abbreviated weekday name. %A Locale's full weekday name. %b Locale's abbreviated month name. %B Locale's full month name. %c Locale's appropriate date and time representation. %I Hour (12-hour clock) as a decimal number [01,12]. %p Locale's equivalent of either AM or PM. """ |
datetime模块
datetime模块提供对于日期和时间进行简单或复杂的操作. datetime 模块提供了一下的可用类型(Available Types)。
- datetime.MINYEAR 和 datetime.MAXYEAR 模块常量表示datetime接受的范围
- class datetime.date: 一个理想化的日期, 提供year, month, day属性
- class datetime.time: 一个理想化的时间, 提供hour, minute, second, microsecond, tzinfo.
- class datetime.datetime: 日期和时间的组合.提供year, month, day, hour, minute, second, microsecond, tzinfo.
- class datetime.timedelta: 表达两个date,time和datetime持续时间内的微妙差异.
- class datetime.tzinfo: 时间对象的抽象基类.
1
2
3
4
|
# 时间相减 import datetime print datetime.datetime.now() print datetime.datetime.now() - datetime.timedelta(days = 5 ) |
最新文章
- myeclipse tomcat内存溢出解决方法
- struts 异常机制
- failed to lazily initialize a collection of role:XXX, no sessi
- python中logging
- Editable DataGrid 实现列表新增编辑功能
- 打开jnlp Faild to validate certificate, the application will not be executed.
- Codeforce Round #211 Div2
- ubuntu11.10(TQ210)下移植boa服务器
- Linux系统的/proc目录
- java之基础数据类型学习————(一)
- python --- 基础多线程编程
- vue webpack配置Error
- window.open模拟表单POST提交
- NIO原理及案例使用
- UVALive - 4225(贪心)
- Event Recommendation Engine Challenge分步解析第二步
- linux poi生成excel demo调试附调用代码
- HDU1853 Cyclic Tour
- SQLServer 学习笔记之超详细基础SQL语句 Part 7
- 一个还不错的gridview 样式【Z】
热门文章
- ElasticSearch(5.5.2)在java中的使用
- D广搜
- Android中个人推崇的数据库使用方式
- 嵌入式开发之davinci---dm8168VPORT口管脚总结
- Android Service 不被杀死并提高优先级
- spring的PROPAGATION_REQUIRES_NEW事务,下列说法正确的是(D)
- LiveWriter插入高亮代码插件介绍 基于SyntaxHighighter
- BootstrapValidator demo
- ABAP WEBRFC
- uboot中关于LCD的代码分析【转】