datetime处理日期和时间

    • datetime.now() # 获取当前datetime
      datetime.utcnow()

     

    • datetime(2017, 5, 23, 12, 20) # 用指定日期时间创建datetime

     

    • 将以下字符串转换成datetime类型:
      • '2017/9/30'
        '2017年9月30日星期六'
        '2017年9月30日星期六8时42分24秒'
        '9/30/2017'
        '9/30/2017 8:42:50 '
    • 将以下datetime类型转换成字符串:
      • 2017年9月28日星期4,10时3分43秒
        Saturday, September 30, 2017
        9/30/2017 9:22:17 AM
        September 30, 2017
    • 用系统时间输出以下字符串:
      • 今天是2017年9月30日
        今天是这周的第?天 
        今天是今年的第?天 
        今周是今年的第?周 
        今天是当月的第?天
      • from datetime import datetime
        s1 =datetime.strptime('2017/9/30','%Y/%m/%d')
        print(s1)
        
        s2 =datetime.strptime('2017年9月30日星期六','%Y年%m月%d日星期六')
        print(s2)
        
        s3 =datetime.strptime('2017年9月30日星期六8时42分24秒','%Y年%m月%d日星期六%H时%M分%S秒')
        print(s3)
        
        s4 =datetime.strptime('9/30/2017','%m/%d/%Y')
        print(s4)
        
        s5 =datetime.strptime('9/30/2017 8:42:50','%m/%d/%Y %H:%M:%S')
        print(s5)
        
        
        d = datetime.now()
        print(d.strftime('%Y年%m月%d日%A%H时%M分%S秒'))
        print(d.strftime('%A,%B %d,%Y'))
        print(d.strftime('%m/%d/%Y %I:%M:%S%p'))
        print(d.strftime('%B %d,%Y'))
        
        print(d.strftime('今天是%Y年%m月%d日'))
        print(d.strftime('今天是这周的第%w天'))
        print(d.strftime('今天是今年的第%j天'))
        print(d.strftime('今周是今年的第%W周'))
        print(d.strftime('今天是当月的第%d天'))

         

posted on 2017-09-30 11:40  104鲍珊珊  阅读(151)  评论(0)    收藏  举报

导航