datetime模块笔记

datetime
-处理时间和日期的标准库
时间戳:从1970年1月1日0时0分0秒,到当前时间的秒数,浮点数,到毫秒
模块:
-date 日期对象, 常用的属性 year,month,day
-time 时间对象, hour, minute, second, microsecond
-datetime 日期时间对象
-timedelta 时间间隔,两个时间之间的长度

"""
 创建  datetime 中的常用对象
"""
#基本用法
from datetime import time,date,datetime,timedelta

d = date(2018,5,6)
print(d,type(d))

t = time(hour=22,minute=34,second=23,microsecond=4567)
print(t,type(t))

dt = datetime(2012,2,22,23,5)
print(dt,type(dt))

td = timedelta(hours=33443.4)
print(td,type(td))

"""
datetime.datetime日期时间对象的常用方法
"""
now = datetime.now()
print(now,type(now))


time_str = now.strftime('%Y-%m-%d,%H:%M:%S,%A %B')
print(time_str,type(time_str))


# dt = datetime.strptime('2018/11/13', '%Y/%m/%d')  
# print(dt, type(dt))

print(now.timestamp())  # 浮点数
# 时间戳-》datetime
dt1 = datetime.fromtimestamp(1542373647.00759)
print(dt1, type(dt1))

#  date time datetime 都是 不可变对象
print({now: 'xinlan'})

"""
时间格式化(记忆)
    %Y  Year with century as a decimal number. 2018 18
    %m  Month as a decimal number [01,12].
    %d  Day of the month as a decimal number [01,31].
    %H  Hour (24-hour clock) as a decimal number [00,23].
    %M  Minute as a decimal number [00,59].
    %S  Second as a decimal number [00,61].
    %z  Time zone offset from UTC.
    %a  Locale's abbreviated weekday name.  星期的简写
    %A  Locale's full weekday name.         星期的全称
    %b  Locale's abbreviated month name.    月的简写
    %B  Locale's full month name.           月的全写
    %c  Locale's appropriate date and time representation.
        # Fri Nov 16 21:18:16 2018
    %I  Hour (12-hour clock) as a decimal number [01,12].
    %p  Locale's equivalent of either AM or PM.
    
"""

'''
时间运算
'''
today = datetime.now().date()
print(today,type(today))

tomorrow = today +timedelta(days=1)
print(tomorrow,type(tomorrow))

运行结果:
在这里插入图片描述
注意时间是不可变对象,tuple表示。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值