您的当前位置:首页正文

Python中的time模块与datetime模块用法总结

来源:九壹网
Python中的time模块与datetime模块⽤法总结

time模块time模块是包含各⽅⾯对时间操作的函数. 尽管这些常常有效但不是所有⽅法在任意平台中有效. time⽤struct_time表⽰时间

import time

# time.struct_time(tm_year=2015, tm_mon=4, tm_mday=24, tm_hour=14, tm_min=17, tm_sec=26, tm_wday=4, tm_yday=114, tm_isdst=0)# 2015

print time.localtime()

print time.localtime().tm_year

函数

time.time(): 返回⼀个时间戳

time.asctime([t]): 转换gmtime()和localtime()返回的元组或struct_time为string.

time.clock(): 在第⼀次调⽤的时候, 返回程序运⾏的时间. 第⼆次之后返回与之前的间隔.

time.ctime([secs]): 将时间戳转换为时间字符串, 如没有提供则返回当前的时间字符串,并与asctime(localtime())⼀样.time.gmtime([secs]): 将时间戳转化为, UTC 时区的struct_time.time.localtime([secs]): 类似gmtime()但会把他转换成本地时区.time.mktime(t): struct_time 转化为时间戳.

time.sleep(secs): 线程推迟指定时间, 以秒为单位.

time.strftime(format[,t]): 根据参数转换⼀个sturc_time或元组为字符串.time.strptime(string[, format]): 与strftime相反,返回⼀个struct_time.

import time

# Fri Apr 24 06:39:34 2015

print time.asctime(time.gmtime())# 0.0# None

# 1.01136392961 因计算机⽽异print time.clock()print time.sleep(1)print time.clock()

# Fri Apr 24 14:42:07 2015print time.ctime()

# 2015-04-24

print time.strftime('%Y-%m-%d', time.localtime())# 1429857836.0

print time.mktime(time.localtime())

time模块中常⽤的格式化字符串

%y 两位数的年份 00 ~ 99.

%Y 四位数的年份 0000 ~ 9999%m ⽉份 01 ~ 12.%d day 01 ~ 31.%H 时 00 ~ 23.%I 时 01 ~ 12.%M 分 00 ~ 59.%S 秒 00 ~ 61.

datetime模块datetime模块提供对于⽇期和时间进⾏简单或复杂的操作. datetime 模块提供了⼀下的可⽤类型(AvailableTypes).

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: 时间对象的抽象基类.

from datetime import timedelta, datetimea = datetime.now()b = timedelta(days=7)# 7 days, 0:00:00

# 2015-04-14 16:02:39.189000print bprint a - b

下⾯说具体说⼀下类和类的⽅法date类

⼀个date对象代表理想化的⽇期.

class datetime.date(year, month, day)

# All arguments are required. Arguments may be ints or longs. # 所有参数都是必须的. 参数可能是 int 或 long. MINYEAR <= year <= MAXYEAR 1<= month <= 12

1<= day <= number of days in the given month and year.(随着⽉份和年份)

如果参数脱离给的范围会抛出, valueError.

1.类⽅法 >`date.today()`:返回当前的本地⽇期, 这等价于 `date.fromtimestamp(time.time())`.Return the current local date. This is equvalent to `date.fromtimestamp(time.time())`.

from datetime import date # print 2015-04-21 print date.today()

2.date.fromtimestamp(timestamp):根据提供的时间戳返回local date. 时间戳常⽤于对时间类型的存储.

import time

from datetime import date

# 1429587111.21# 2015-04-21print time.time()

print date.fromtimestamp(time.time())

3.类⽅法date.fromordinal(ordinal):根据提供的Gregorian⽇历返回date.(不做描述)类属性

date.min: 返回 date(MINYEAR, 1, 1).date.max: 返回 date(MAXYEAR, 12, 31).

date.year: 返回 年, MINYEAR和MAXYEAR之间date.month: 返回 ⽉, 1到12⽉之间date.day: 返回 1到 n 之间.

d = date(2014, 4, 21)# 2014 4 21

print d.year, d.month, d.day

实例⽅法

date.replace(year, month, day):返回⼀个相同值的data对象, 除了这些参数给关键字指定新的值.date.timetuple(): 返回⼀个time.struct_time对象.date.toordinal(): 返回⼀个Gregoian Calendar对象.

date.weekday(): 返回day of the week. 星期⼀为0,星期⽇为6.date.isoweekday(): 返回day of the week. 星期⼀为1,星期⽇为7.

date.isocalendar(): 返回⼀个三元组, (ISO year, ISO week number, ISO weekday).

date.isoformat(): 返回 ⼀个'YYYY-MM-DD'的字符串格式.

date.ctime(): 返回⼀个字符串⽇期, d.ctime() 等同于 time.ctime(time.mktime(d.timetuple())).date.strftime(format): 返回⼀个字符串⽇期, 格式⾃定义.

d = date(2015, 4, 21)# 2015-04-21# 2015-04-21# 2015-04-22print d

print d.replace()

print d.replace(day=22)

# time.struct_time(tm_year=2015, tm_mon=4, tm_mday=21, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=111, tm_isdst=-1)print d.timetuple()# print 1# print 2

print d.weekday()print d.isoweekday()# print 2015-04-21print d.isoformat()

# print 21/04/2015

print d.strftime('%d/%m/%y')

datetime 类

datetime 对象是⼀个单⼀的对象, 包含所有date和time对象的信息.

class datetime.datetime(year, month, day[, hour [, minute [, second

[, microsecond [, tzinfo]]]]])

# The year, month and day arguments are required. MINYEAR <= year <= MAXYEAR 1 <= month <= 12 1 <= day <= n 0 <= hour < 24 0 <= minute < 60 0 <= second < 60

0 <= microsecond < 10**6

类⽅法

datetime.today(): 返回当前本地datetime.随着 tzinfo None. 这个等同于datetime.fromtimestamp(time.time()).datetime.now([tz]): 返回当前本地⽇期和时间, 如果可选参数tz为None或没有详细说明,这个⽅法会像today().datetime.utcnow(): 返回当前的UTC⽇期和时间, 如果tzinfo None ,那么与now()类似.datetime.fromtimestamp(timestamp[, tz]): 根据时间戳返回本地的⽇期和时间.tz指定时区.datetime.utcfromtimestamp(timestamp): 根据时间戳返回 UTC datetime.datetime.fromordinal(ordinal): 根据Gregorian ordinal 返回datetime.datetime.combine(date, time): 根据date和time返回⼀个新的datetime.

datetime.strptime(date_string, format): 根据date_string和format返回⼀个datetime.

from datetime import datetime# 2015-04-21 14:07:39.262000print datetime.today()# 2015-04-21 14:08:20.362000print datetime.now()

# 1429596607.06

# 2015-04-21 14:10:07.061000t = time.time() print t

print datetime.fromtimestamp(t)

from datetime import datetime, date, timea = date(2015, 4, 21)

b = time(14, 13, 34)# 2015-04-21 14:13:34

print datetime.combine(a, b)

实例⽅法

datetime.date(): 返回相同年⽉⽇的date对象.datetime.time(): 返回相同时分秒微秒的time对象.

datetime.replace(kw): kw in [year, month, day, hour, minute, second, microsecond, tzinfo], 与date类似.其他⽅法可查看官⽅⽂档…

from datetime import datetime, date, timetd = date(2015, 4, 21)n = time(14, 28, 30)

# 2099-04-21 14:30:42.103000

print datetime.now(0.replace(year=2099)

类属性

datetime.min: datetime(MINYEAR, 1, 1).

datetime.max: datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999).实例属性(read-only)

datetime.year: 1 ⾄ 9999datetime.month: 1 ⾄ 12datetime.day: 1 ⾄ n

datetime.hour: In range(24). 0 ⾄ 23datetime.minute: In range(60).datetime.second: In range(60).

datetime.microsecond: In range(1000000).time类

time 代表本地(⼀天内)时间.

class datetime.time([hour [, minute [, second

[, microsecond [, tzinfo]]]]])

# All arguments are optional. # 所有参数都是可选的. 0 <= hour < 24 0 <= minute < 60 0 <= second < 60

0 <= microsesond < 10**6

time类就是对时间的⼀些操作,其功能类似与datetime.其实date和time就是对datetime中⽇期和时间的操作.

因篇幅问题不能全部显示,请点此查看更多更全内容

Top