[Python] Python时间操作

Python的时间操作 date, time, datetime

大部分是依据参考文献来进行举例的。
主要分为3部分,只针对date的操作,只针对time的操作,针对datetime的操作。
1. 针对date的操作

#!/usr/bin/env python
#-*- coding:UTF-8 -*-

from datetime import *
import time

'''日期由年月日组成'''
'''构造函数'''
now = date(2016,6,13)
'''对象所能表示的最大、最小日期'''
print(date.max)
'''date对象表示日期的最小单位'''
print(date.resolution)
'''返回一个表示当前本地日期的date对象'''
print(date.today())
'''根据给定的时间戮,返回一个date对象'''
print(date.fromtimestamp(1))
'''将Gregorian日历时间转换为date对象.'''
print(date.fromordinal(now.toordinal()))

'''常用方法'''
'''year, month, day'''
print("now.year="+str(now.year)+" , now.month="+str(now.month)+
      " , now.day="+str(now.day))
'''replace生成一个新的日期对象,用参数指定的年,月,日代替原有对象中的属性。'''
print("now.replace(1990,5,11)="+str(now.replace(1990,5,11)))
print("now="+str(now))
'''返回日期对应的time.struct_time对象.'''
print(" now.timetuple()="+str(now.timetuple()))
'''返回日期对应的Gregorian Calendar日期'''
print("now.toordinal()="+str(now.toordinal()))
'''返回weekday,如果是星期一,返回0,如果是星期2,返回1,以此类推'''
print("now.weekday()="+str(now.weekday()))
'''返回weekday,如果是星期一,返回1,如果是星期2,返回2,以此类推'''
print("now.isoweekday()="+str(now.isoweekday()))
'''返回格式如'YYYY-MM-DD' 的字符串'''
print("now.isoformat()="+str(now.isoformat()))
'''自定义格式化字符串'''
print("now.strftime(fmt)="+str(now.strftime("%F")))
'''日期比较'''
print("1970<2016="+str(date.fromtimestamp(1)<now))

需要注意上面的自定义strftime的时候,可以参考strftime的使用,里面介绍了详细的格式。

2.针对time的操作,这个UTC类是时区信息,需要自定义如参考文献

#!/usr/bin/env python
#-*- coding:UTF-8 -*-

from datetime import *

class UTC(tzinfo):
    def __init__(self,offset=0):
        self._offset = offset

    def utcoffset(self,dt):
        return timedelta(hours=self._offset)
    def tzname(self,dt):
        return "UTC +%s" %self._offset
    def dst(self,dt):
        return timedelta(hours=self._offset)

'''构造函数 类表示时间,由时、分、秒以及微秒组成+时区'''
tm = time(8,8,8,8,tzinfo=UTC(8))
print("time="+str(tm))
'''time类所能表示的最小、最大时间'''
print("time.max()="+str(time.max))
'''时间的最小单位,这里是1微秒'''
print("time.resolution()="+str(time.resolution))
print("time.hour()="+str(tm.hour))
'''时区信息'''
print("time.tzinfo="+str(tm.tzinfo))
'''返回型如"HH:MM:SS"格式的字符串表示.'''
print("tm.isoformat="+tm.isoformat())
'''返回自定义格式化字符串'''
print("tm.strftime(fmt)="+tm.strftime("%T"))

strftime的格式参考上面的date操作

3.Datetime操作,是date 和time的结合体,注意其中的string转化为datetime的函数。

#!/usr/bin/env python
#-*- coding:UTF-8 -*-

from datetime import *

'''够造函数'''
dt = datetime(1990,5,11,8,8,8,8)
print("dt = "+str(dt))
''' 返回一个表示当前本地时间的datetime对象;'''
print("datetime.today()="+str(datetime.today()))
''' 返回一个当前utc时间的datetime对象'''
print("datetime.utcnow()="+str(datetime.utcnow()))
''' 根据时间戮创建一个datetime对象'''
print("datetime.fromtimestamp(1)="+str(datetime.fromtimestamp(1)))
''' 根据时间戮创建一个datetime对象;'''
print("datetime.utcfromtimestamp(1)"+str(datetime.utcfromtimestamp(1)))
''' 根据date和time,创建一个datetime对象'''
day = date(2016,6,13)
tm = time(8,8,8,8)
print("datetime.combine(date, time)"+str(datetime.combine(day, tm)))
''' 将格式字符串转换为datetime对象'''
print("datetime.strptime(date_string, format)="+ \
      str(datetime.strptime("1990-05-11 10:43:39", "%Y-%m-%d %H:%M:%S")))
''' 实例方法与属性'''
print("datetime.year="+str(dt.year))
''' 获取time对象'''
print("datetime.time()="+str(dt.time()))
''' '''
print("datetime.timetuple()="+str(dt.timetuple()))
''' Gegorial时间'''
print("datetime.toordinal()="+str(dt.toordinal()))
''' ctime格式'''
print("datetime.ctime()="+str(dt.ctime()))
''' 输出ctime格式'''
print("datetime.strftime(fmt)="+str(dt.strftime("%c")))

猜你喜欢

转载自blog.csdn.net/xsjyahoo/article/details/51655838