python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python的time与datetime模块

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

作者:cangmean

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

函数

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 2015
print time.ctime()

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

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

datetime模块
datetime模块提供对于日期和时间进行简单或复杂的操作. datetime 模块提供了一下的可用类型(Available Types).

datetime.MINYEAR 和 datetime.MAXYEAR 模块常量表示datetime接受的范围

from datetime import timedelta, datetime

a = datetime.now()
b = timedelta(days=7)

# 7 days, 0:00:00
# 2015-04-14 16:02:39.189000
print b
print 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-21
print time.time()
print date.fromtimestamp(time.time())

3.类方法date.fromordinal(ordinal):根据提供的Gregorian日历返回date.(不做描述)

类属性

d = date(2014, 4, 21)
# 2014 4 21
print d.year, d.month, d.day

实例方法

d = date(2015, 4, 21)

# 2015-04-21
# 2015-04-21
# 2015-04-22
print 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-21
print 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

类方法

from datetime import datetime

# 2015-04-21 14:07:39.262000
print datetime.today()

# 2015-04-21 14:08:20.362000
print datetime.now()

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

from datetime import datetime, date, time

a = date(2015, 4, 21)
b = time(14, 13, 34)

# 2015-04-21 14:13:34
print datetime.combine(a, b)

实例方法

其他方法可查看官方文档…

from datetime import datetime, date, time

td = date(2015, 4, 21)
n = time(14, 28, 30)

# 2099-04-21 14:30:42.103000
print datetime.now(0.replace(year=2099)

类属性

实例属性(read-only)

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中日期和时间的操作.

您可能感兴趣的文章:
阅读全文