欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python 时间转换

程序员文章站 2022-06-24 08:59:42
## import datetime# from datetime import datetime# import time## now_time = datetime.now()# # now:2019-07-31 15:05:54.306301# print('now:{0}'.format(d ......
#
# import datetime
# from datetime import datetime
# import time
#
# now_time = datetime.now()
# # now:2019-07-31 15:05:54.306301
# print('now:{0}'.format(datetime.now()))
#
# # 当前的日期
# # now day:2019-07-31
# print('now day:{0}'.format(now_time.date()))
#
# # 当前的时间
# # now time:15:07:54.550967
# print('now time:{0}'.format(now_time.time()))
#
# # 当前年份
# # now year:2019
# print('now year:{0}'.format(now_time.year))
#
# # 当前月份
# # now month:7
# print('now month:{0}'.format(now_time.month))
#
# # 当前日期
# # now day:31
# print('now day:{0}'.format(now_time.day))
#
#
# # 1564557243.8810754 获取到毫秒
# print(time.time())




from datetime import datetime,date,time,timedelta
# 自定义日期和时间
# d = datetime(2020,10,30,14,5)
# # 2020-10-30 14:05:00
# print(d)
#
# d2 = date(2019,3,23)
# # 2019-03-23
# print(d2)
#
# t = time(9,0)
# # 09:00:00
# print(t)

print('---------------------------------')
# # 日期 时间 字符串之间的相互转换
# ds = '2018-10-3 13:42:09'
# ds_t = datetime.strptime(ds, '%y-%m-%d %h:%m:%s')
# # 9
# print(ds_t.second)


# print('---------------------------------')
# # datetime对象转换成字符串
# n = datetime.now()
# n_str = n.strftime('%y-%m-%d %h:%m:%s')
# # 2019-07-31 15:32:21
# print(n_str)


# print('---------------------------------')
# # datetime对象 加减操作
# n = datetime.now()
# # 2019-07-31 15:37:15.269619
# print(n)
# n_next = n + timedelta(days = 5, hours = 42)
# # 2019-08-07 09:37:15.269619
# print(n_next)

# 时间的减法
d1 = datetime(2018,10,15)
d2 = datetime(2018,11,12)

rest = d2 - d1
print(dir(rest))

# 28 相差天数
print(rest.days)