Python和时间相关的模块有2个:time、datetime
time模块,常用的方法
1)time()方法:返回UTC时间1970年1月1日至今有多少秒
time(...)
time() -> floating point number
Return the current time in seconds since the Epoch.
Fractions of a second may be present if the system clock provides them.
2)localtime()方法:返回当地时区时间,参考gmtime
localtime(...)
localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,
tm_sec,tm_wday,tm_yday,tm_isdst)
Convert seconds since the Epoch to a time tuple expressing local time.
time.localtime(time.time())
time.struct_time(tm_year=2013, tm_mon=1, tm_mday=13, tm_hour=17, tm_min=52, tm_sec=26, tm_wday=6, tm
_yday=13, tm_isdst=0)
3)strftime():按指定format格式化时间成字符串
strftime(...)
strftime(format[, tuple]) -> string
Convert a time tuple to a string according to a format specification.
time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
'2013-01-13 17:58:39'
python中时间日期格式化符号:
%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00=59)
%S 秒(00-59)
%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366)
%p 本地A.M.或P.M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身
4)sleep():线程休眠多少秒
sleep(...)
sleep(seconds)
Delay execution for a given number of seconds. The argument may be
a floating point number for subsecond precision.
datetime模块,使用它可以获取毫秒时间,感觉要比time强大
1)today()和now():获取当前时间
d = datetime.datetime
d.now()
datetime.datetime(2013, 1, 13, 18, 39, 19, 903000)
d.now().microsecond
93000
2)strftime()格式化时间
d.now().strftime('%Y-%m-%d %H:%M:%S')
'2013-01-13 18:42:14'
除此之外,还有calendar模块和日期时间有关
calendar模块:This module allows you to output calendars like the Unix cal program, and provides additional useful functions related to the calendar. By default, these calendars have Monday as the first day of the week, and Sunday as the last (the European convention). Use setfirstweekday() to set the first day of the week to Sunday (6) or to any other weekday. Parameters that specify dates are given as integers. For related functionality, see also the datetime and time modules.
c = calendar.TextCalendar()
c.setfirstweekday(6)
c.prmonth(2013,1)
-----------------------
January 2013
Su Mo Tu We Th Fr Sa
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
分享到:
相关推荐
PyTime:一个简单易用的Python模块,用于通过字符串来操作日期/时间
主要介绍了Python日期时间Time模块,结合实例形式详细分析了Python日期时间Time模块常用函数及相关操作技巧,需要的朋友可以参考下
用C语言编写的用于Python的快速ISO8601日期时间解析器。由于它是作为C模块编写的,所以它比其他Python库快得多。 用Python 2.7 3.4 3.5测试。
整理总结了Python语言函数的定义,类与对象的定义与使用,Python数据类型转换函数,random函数,判断输入是否为数字判断相关函数,Python取整的方式,常见的Python的数学函数,时间日期相关函数,以及Python的一种...
Python中的列表(List)和元组(Tuple)有什么区别?...Python中如何处理日期和时间? 什么是Python中的Lambda函数?如何使用Lambda函数? Python中如何进行字符串格式化? 什么是Python中的面向对象编程(OOP)?列
操作系统功能,包含线程与进程支持、IO复用、日期与时间处理、调用系统函数、写日记(logging)等功能 网络通信,包含网络套接字,SSL加密通信、异步网络通信等功能 网络协议,支持HTTP,FTP,SMTP,POP,IMAP,NNTP,...
标准库中的`datetime`模块提供了日期和时间操作的基本功能,包括创建日期时间对象、日期时间比较、格式化等。`time`模块则专注于处理时间相关操作,如获取当前时间戳、格式化时间等。另外,`calendar`模块提供了处理...
Python 日期和时间及模块函数
Python 对于日期时间的处理总共有三个模块:datetime 模块、time 模块、Calendar 模块
该模块提供了五种核心对象:datetime时间日期类型,date日期类型,time时间类型,tzinfo时区类型,timedelta时间差类型,今天为大家介绍一下datetime模块的具体使用方法与python日期时间计算与比较的相关实例 一、Python...
Python中提供了多个用于对日期和时间进行操作的内置模块:time模块、datetime模块和calendar模块。其中time模块是通过调用C库实现的,所以有些方法在某些平台上可能无法调用,但是其提供的大部分接口与C标准库time.h...
Time模块(函数);1、time.asctime([tupletime]) 接受时间元组并返回一个可读的形式为"Tue Dec 11 18:07:14 2008“ (2008年12月11日?周二18时07分14秒)的24个字符的字符串。 ;2、time.gmtime([secs]) 接收时间戳...
freezegun:让您的Python测试穿越时空的感觉,通过伪造日期模块来生成不同的时间
Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间 时间间隔是以秒为单位的浮点小数。 每个时间戳都以自从 1970 年 1 月 1 日午夜(历元)经过了多长时间来表示。;time 模块下有很多函数可以转换...
下面我们先简单的了解下python日期和时间数据类型及工具 给datetime对象加上或减去一个或多个timedelta,会产生一个新的对象 from datetime import datetime from datetime import timedelta now = datetime.now() ...
一、Python常用的内置标准模块 二、Python常用扩展库 Python提供了大约200多个内置的标准模块,涵盖了Python运行时服务、文字模式匹配、操作系统接口、数学运算、对象永久保存、网络和Internet脚本和GUI构建等...
Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能。 Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间。