zoukankan      html  css  js  c++  java
  • datetime处理日期和时间

    • datetime.now() # 获取当前datetime
      datetime.utcnow()
    from datetime import datetime
    dt=datetime.now()
    print(dt)
    dt=datetime.utcnow()
    print(dt)

    • datetime(2017, 5, 23, 12, 20) # 用指定日期时间创建datetime
    dt=datetime(2017,9,28,10,3,43)

    将以下字符串转换成datetime类型:

    '2017/9/30'
    '2017年9月30日星期六'
    '2017年9月30日星期六8时42分24秒'
    '9/30/2017'
    '9/30/2017 8:42:50 '

    from datetime import datetime
    st=datetime.strptime('2017年9月30日星期六','%Y年%m月%d日星期六')
    print(st)
    st=datetime.strptime('2017-09-30 08:42:05','%Y-%m-%d %H:%M:%S')
    print(st)
    st=datetime.strptime('2017/9/30','%Y/%m/%d')
    print(st)
    st=datetime.strptime('2017年9月30日星期六8时42分24秒','%Y年%m月%d日星期六%H时%M分%S秒')
    print(st)
    st=datetime.strptime('9/30/2017','%m/%d/%Y')
    print(st)
    st=datetime.strptime('9/30/2017 8:42:50 ','%m/%d/%Y %H:%M:%S ')
    print(st)

    将以下datetime类型转换成字符串:

    2017年9月28日星期4,10时3分43秒
    Saturday, September 30, 2017
    9/30/2017 9:22:17 AM
    September 30, 2017

    dt=datetime(2017,9,28,10,3,43)
    print(dt.strftime('%Y年%m月%d日%A,%H时%M分%S秒'))
    print(dt.strftime('%A,%B %d,%Y'))
    print(dt.strftime('%m/%d/%Y %I:%M:%S%p'))
    print(dt.strftime('%B %d,%Y'))

    用datetime类型的变量输出以下字符串:

    今天是2017年9月30日
    今天是这周的第?天 
    今天是今年的第?天 
    今周是今年的第?周 
    今天是当月的第?天

    dt=datetime(2017,9,30)
    
    print(dt.strftime('今天是%Y年%m月%d日'))
    
    print(dt.strftime('今天是这周的第%w天'))
    
    print(dt.strftime('今天是今年的第%j天'))
    
    print(dt.strftime('今周是今年的第%W周'))
    
    print(dt.strftime('今天是当月的第%d天'))

  • 相关阅读:
    Codeforces 678E 状压DP
    Codeforces 667C DP
    POJ 3017 DP + 单调队列 + 堆
    Codeforces 1154F (DP)
    Codeforces 1154G 枚举
    Codeforces 1153D 树形DP
    Codeforces 1109E 线段树
    Codeforces 1109C 线段树
    Codeforces 1109D (树的计数问题)
    async/await
  • 原文地址:https://www.cnblogs.com/lintingting/p/7613791.html
Copyright © 2011-2022 走看看