码迷,mamicode.com
首页 > 其他好文 > 详细

时间模块 time

时间:2019-07-18 20:03:47      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:date   import   时间戳   等价   iso   计算   res   day   imp   

"""
时间模块
import time
三种表现形式
    1.时间戳
    2.格式化时间(用来展示给人看的)
    3. 结构化时间

"""
# import time
# print(time.time())#显示的是从1970:1月1日凌晨00:00到现在的时间
# #结果:1563446269.2257721
# print(time.strftime("%Y-%m-%d"))
# #结果:2019-07-18 #显示的是当前时间
# print(time.strftime("%Y-%m-%d %H:%M:%S" ))
# #结果显示的是2019-07-18 18:43:09 显示的是年月日 小时分钟秒
# print(time.strftime("%X"))
# #结果 %X就等价于%H:%M:%S  18:45:23

import datetime
print(datetime.date.today())
#结果显示 data>>显示的是年月日
print(datetime.datetime.today())
#结果显示 datetime >>显示的是年月日 时分秒
res = datetime.date.today()
print(res)  # 用res接受打印结果和上面一样
print(res.year)  #打印 年
print(res.month)  # 打印月
print(res.day)    # 打印日
print(res.weekday())  #打印星期几 国外0-6  0 表示周一
print(res.isoweekday()) #打印星期几  1-7 7 表示的就是周日

"""
日期对象 = 日期对象+/- timedelta 对象
timedelta对象 = 日期对象 +/- 日期对象


"""
current_time = datetime.date.today()  # 日期对象
# timetel_t = datetime.timedelta(days=7)  # timedelta对象
# res1 = current_time+timetel_t  # 日期对象
#
# print(current_time - timetel_t)
# print(res1-current_time)


# 小练习 计算今天距离今年过生日还有多少天
# birth = datetime.datetime(2019,12,21,8,8,8)
# current_time = datetime.datetime.today()
# print(birth-current_time)

# UTC时间
# dt_today = datetime.datetime.today()
# dt_now = datetime.datetime.now()
# dt_utcnow = datetime.datetime.utcnow()
# print(dt_utcnow,dt_now,dt_today)

 

时间模块 time

标签:date   import   时间戳   等价   iso   计算   res   day   imp   

原文地址:https://www.cnblogs.com/yangxinpython/p/11209381.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!