Python【datetime】模块

import datetime
print("==============date类================")
#创建一个date对象:datetime.date(year, month, day)
#1、静态方法和字段
# date.max、date.min:date对象所能表示的最大、最小日期;
# date.resolution:date对象表示日期的最小单位。这里是天。
# date.today():返回一个表示当前本地日期的date对象;
# date.fromtimestamp(timestamp):根据给定的时间戮,返回一个date对象;

print(datetime.date(2018,5,3).max)
print('date.max:', datetime.date.max)
print('date.min:', datetime.date.min)
print('date.today():', datetime.date.today())
#print('date.fromtimestamp():', datetime.date.fromtimestamp(time.time())) #time.time()是一个float类型
print('date.fromtimestamp():', datetime.date.fromtimestamp(1525341374.7134962))


#2、方法和属性
d1 = datetime.date(2018,5,3)#date对象
print(d1.year,d1.month,d1.day) #年、月、日;
print(d1.replace(2018, 5, 8)) #生成一个新的日期对象,用参数指定的年,月,日代替原有对象中的属性。(原有对象仍保持不变)
print(d1.timetuple()) #返回日期对应的time.struct_time对象;
print(type(d1.weekday()),d1.weekday())#返回weekday,如果是星期一,返回0;如果是星期2,返回1,以此类推;
print(type(d1.isoweekday()),d1.isoweekday()) #返回weekday,如果是星期一,返回1;如果是星期2,返回2,以此类推;
print(type(d1.isocalendar()),d1.isocalendar()) #返回格式如(year,month,day)的元组;
print(d1.isoformat()) #返回格式如'YYYY-MM-DD’的字符串;
print(d1.strftime("%Y-%m-%d")) #和time模块format相同。


now = datetime.date(2018, 5, 6)
tomorrow = now.replace(day = 27)
print('now:', now, ', tomorrow:', tomorrow)
print('timetuple():', now.timetuple())
print('weekday():', now.weekday())#0-6表示星期一到星期日
print('isoweekday():', now.isoweekday())#1-7表示星期一到星期日
print('isocalendar():', now.isocalendar())
print('isoformat():', now.isoformat())
print('strftime():', now.strftime("%Y-%m-%d"))

print("=============time类============")
#datetime.time(hour[ , minute[ , second[ , microsecond[ , tzinfo] ] ] ] )

#1、静态方法和字段
#time类所能表示的最小、最大时间。其中,time.min = time(0, 0, 0, 0), time.max = time(23, 59, 59, 999999);
print("最小值:",datetime.time.min,"最大值:",datetime.time.max)
#时间的最小单位,这里是1微秒;
print(datetime.time.resolution)

#2、方法和属性
t1 = datetime.time(10,23,15)#time对象
print(t1.hour,t1.minute,t1.second,t1.microsecond) #时、分、秒、微秒;
print(t1.tzinfo) #时区信息;
#创建一个新的时间对象,用参数指定的时、分、秒、微秒代替原有对象中的属性(原有对象仍保持不变)
#print(t1.replace(,))
print(t1.isoformat()) #返回型如"HH:MM:SS"格式的字符串表示;
print(t1.strftime("%X")) #同time模块中的format;

tm = datetime.time(23, 46, 10)
print('tm:', tm)
print('hour: %d, minute: %d, second: %d, microsecond: %d' % (tm.hour, tm.minute, tm.second, tm.microsecond))
tm1 = tm.replace(hour=20)
print('tm1:', tm1)
print('isoformat():', tm.isoformat())
print('strftime()', tm.strftime("%X"))


print("=============datetime类=============")
# datetime相当于date和time结合起来。
# datetime.datetime (year, month, day[ , hour[ , minute[ , second[ , microsecond[ , tzinfo] ] ] ] ] )
#1、静态方法和字段
# datetime.today():返回一个表示当前本地时间的datetime对象;
# datetime.now([tz]):返回一个表示当前本地时间的datetime对象,如果提供了参数tz,则获取tz参数所指时区的本地时间;
# datetime.utcnow():返回一个当前utc时间的datetime对象;#格林威治时间
# datetime.fromtimestamp(timestamp[, tz]):根据时间戮创建一个datetime对象,参数tz指定时区信息;
# datetime.utcfromtimestamp(timestamp):根据时间戮创建一个datetime对象;
# datetime.combine(date, time):根据date和time,创建一个datetime对象;
# datetime.strptime(date_string, format):将格式字符串转换为datetime对象;
print('datetime.max:', datetime.datetime.max)
print('datetime.min:', datetime.datetime.min)
print('datetime.resolution:', datetime.datetime.resolution)
print('today():', datetime.datetime.today())
print('now():', datetime.datetime.now())
print('utcnow():', datetime.datetime.utcnow())
print('fromtimestamp(tmstmp):', datetime.datetime.fromtimestamp(1525345689.8390145))
print('utcfromtimestamp(tmstmp):', datetime.datetime.utcfromtimestamp(1525345689.8390145))
#2、方法和属性
dt=datetime.datetime.now()#datetime对象
print(dt.year,dt.month,dt.day,dt.hour,dt.minute,dt.second,dt.microsecond,dt.tzinfo)
print(dt.date()) #获取date对象;
print(dt.time()) #获取time对象;
print(dt.replace(year=2017))
print(dt.timetuple())
print(dt.utctimetuple())
print(dt.toordinal())
print(dt.weekday())
print(dt.isocalendar())
#print(dt.isoformat ([ sep] ))
print(dt.ctime()) #返回一个日期时间的C格式字符串,等效于time.ctime(time.mktime(dt.timetuple()));
print(dt.strftime("%Y-%m-%d"))


print("==========timedelta类,时间加减===========")
#使用timedelta可以很方便的在日期上做天days,小时hour,分钟,秒,毫秒,微妙的时间计算,如果要计算月份则需要另外的办法。
dt = datetime.datetime.now()
#日期减一天
dt1 = dt + datetime.timedelta(days=-1)#昨天
dt2 = dt - datetime.timedelta(days=1)#昨天
dt3 = dt + datetime.timedelta(days=3)#明天
print(type(dt1),type(dt2),type(dt3),dt1,dt2,dt3)
print(datetime.timedelta(days=3))
delta_obj = dt3-dt
print(type(delta_obj),delta_obj)#<class 'datetime.timedelta'> 3 days, 0:00:00
print(delta_obj.days ,delta_obj.total_seconds())#1 86400.0
posted @ 2018-05-10 21:58  一分耕耘,一分收获  阅读(173)  评论(0编辑  收藏  举报