import datetime
import time
'''
datetime模块包含的类
date 日期
time 时间
datetime 日期时间
timedelta 时间间隔
tzinfo 时区
'''
'''
datetime.date(year, month, day)
获取日期对象
'''
date_obj = datetime.date(2020, 4, 1)
print(type(date_obj)) # <class 'datetime.date'>
'''
datetime.date.fromtimestamp(seconds)
根据时间戳获去日期对象
'''
time_stamp = time.time()
print(datetime.date.fromtimestamp(time_stamp)) # 2020-04-01
'''
datetime.date.replace(year, month, day)
替换原来的date对象并生成一个新的date对象
'''
date1 = datetime.date(2019, 4, 1)
date2 = date1.replace(2020, 4, 1)
print(date2) # 2020-04-01
'''
datetime.date.timetuple()
返回date对应的时间元组
'''
date = datetime.date(2020, 4, 1)
print(date.timetuple())
# time.struct_time(tm_year=2020, tm_mon=4, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=92, tm_isdst=-1)
'''
datetime.date.weekday()
返回date是星期几,从0开始,星期一为0
'''
today = datetime.date.today()
print(today.weekday())
'''
datetime.date.isoweekday()
返回date是星期几,从1开始,星期一是1
'''
today = datetime.date.today()
print(today.isoweekday()) # 3
'''
datetime.date.isocalendar()
返回年,一年中的第几周和一周中的第几天(从1开始,星期一为1)
'''
today = datetime.date.today()
print(today.isocalendar()) # (2020, 14, 3)
'''
datetime.date.isoformat()
返回日期的标准格式字符串
'''
print(datetime.date.today().isoformat()) # 2020-04-01
'''
datetime.time(hour,minute,second,microsecond,tzinfo)
hour---小时
minute---分
second---秒
microsecond---微妙,百万分之一秒
tzinfo---时区信息
'''
time_ = datetime.time(14,36,30,500)
print(time_) # 14:36:30.000500
print(time_.hour) # 14
print(time_.minute) # 36
print(time_.second) # 30
print(time_.microsecond) # 500
print(time_.tzinfo) # None
'''
datetime.time.replace(hour, minute, second, microsecond)
替换原来的time生成一个新的time
'''
time1 = datetime.time(10, 15, 36, 7000)
time2 = time1.replace(12, 16, 27, 4000)
print(time2) # 12:16:27.004000
'''
datetime.time.isoformat()
返回标准的时间字符串
'''
time_ = datetime.time(14, 46, 36, 100)
print(time_.isoformat()) # 14:46:36.000100
'''
datetime.time.strftime(format)
返回一个定制的时间字符串
'''
time_ = datetime.time(14, 49, 50)
print(time_.strftime('%H:%M:%S')) # 14:49:50
'''
datetime.datetime(year,month,day,hour,minute,second,microsecond)
'''
date_time = datetime.datetime(2020,4,1,14,51,30,4111)
print(date_time) # 2020-04-01 14:51:30.004111
'''
datetime.datetime.today()
获取当前的日期时间对象
'''
print(datetime.datetime.today()) # 2020-04-01 14:52:41.644794
'''
datetime.datetime.now(tz=None)
返回当前的日期时间对象,可以接收tz(时区参数),如果不指定参数,则结果和datetime.datetime.today()一样
'''
print(datetime.datetime.now()) # 2020-04-01 14:54:59.362091
'''
datetime.datetime.utcnow()
返回格林威治日期时间对象
'''
print(datetime.datetime.utcnow()) # 2020-04-01 06:56:06.057328
'''
datetime.datetime.fromtimestamp(seconds)
将时间戳转化为日期时间对象
'''
print(datetime.datetime.fromtimestamp(time.time())) # 2020-04-01 14:59:04.920301
'''
datetime.datetime.isoformat(sep='T')
返回标准格式的日期时间字符串,sep为分割符,默认为T
'''
print(datetime.datetime.today().isoformat(sep=' ')) # 2020-04-01 15:01:28.531929
'''
datetime.datetime.ctime()
返回本地表示的时间
'''
print(datetime.datetime.today().ctime()) # Wed Apr 1 15:02:41 2020
'''
datetime.datetime.strftime(format)
将日期时间对象转化为定制的日期时间字符串
'''
print(datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S')) # 2020-04-01 15:04:38
'''
datetime.datetime.strptime(date_string, format)
将定制的日期时间字符串转化为日期时间对象
date_string为日期时间字符串
format为定制的格式
'''
print(datetime.datetime.strptime('2020-4-1 15:07:30', '%Y-%m-%d %H:%M:%S')) # 2020-04-01 15:07:30
'''
datetime.datetime.combine(date, time)
将date对象和时间对象合并成一个datetime对象
'''
date_ = datetime.date.today()
time_ = datetime.time(15, 10, 30)
print(datetime.datetime.combine(date_, time_)) # 2020-04-01 15:10:30
'''
datetime.datetime.timetuple()
将datetime对象转化为时间元组
'''
date_time = datetime.datetime.today()
print(date_time.timetuple())
# time.struct_time(tm_year=2020, tm_mon=4, tm_mday=1, tm_hour=15, tm_min=11, tm_sec=49, tm_wday=2, tm_yday=92, tm_isdst=-1)
'''
datetime.datetime.utctimetuple()
将datetime对象转化为utc标准的时间元组
'''
date_time = datetime.datetime.today()
print(date_time.utctimetuple())
# time.struct_time(tm_year=2020, tm_mon=4, tm_mday=1, tm_hour=15, tm_min=13, tm_sec=24, tm_wday=2, tm_yday=92, tm_isdst=0)
'''
datetime.datetime.toordinal()
计算datetime是从0001-01-01开始算起的第几天
'''
print(datetime.datetime.today().toordinal()) # 737516
'''
datetime.datetime.weekday()
返回datetime对象是星期几,从0开始,星期一是0
'''
print(datetime.datetime.today().weekday()) # 2
'''
datetime.datetime.isocalendar()
返回datetime对象的年份,一年的第几天,一周的第几天,结果为元组
'''
print(datetime.datetime.today().isocalendar()) # (2020, 14, 3)
'''
datetime.timedelta(days, seconds, microseconds, milliseconds, minutes, hours, weeks)
'''
time_delta = datetime.timedelta(days=5, minutes=3)
print(time_delta) # 5 days, 0:03:00
'''
利用timedelta对datetime对象进行计算
'''
today = datetime.datetime.today()
time_delta = datetime.timedelta(days=30, hours=5, minutes=4)
future_day = today + time_delta
print(future_day) # 2020-05-01 20:27:16.940289
datetime1 = datetime.datetime(2020, 4, 1, 14, 26, 48)
datetime2 = datetime.datetime(2019, 4, 1, 19, 35, 37)
time_delta = datetime1 - datetime2
print(time_delta) # 365 days, 18:51:11
python基础模块---datetime
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- time模块 time模块中时间表现的格式主要有三种: a、timestamp时间戳,时间戳表示的是从1970年1...
- 实践检验真知 以下代码直接复制到文件运行 1. time 模块详解 2. datetime 模块详解 3.date...
- 1 操作文档 http://python-packaging.readthedocs.io/en/latest/m...
- time模块 主要包含处理年月日时分秒对应的时间 1.获取当前时间 获取到的为时间戳。 时间戳:从格林威治时间(从...
- 1.datetime模块(用于修改日期) 2.random模块 1.随机整数函数 1.randint randin...