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

    1、datetime.now() # 获取当前datetime

         datetime.utcnow()

    from datetime import datetime
    datetime.now()

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

    from datetime import datetime
    dt=datetime(2017, 5, 23, 12, 20)
    print(dt)

    3、将以下字符串转换成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
    time1=datetime.strptime('2017/9/30','%Y/%m/%d')
    print(time1)
    time2=datetime.strptime('2017年9月30日星期六','%Y年%m月%d日星期六')
    print(time2)
    time3=datetime.strptime('2017年9月30日星期六8时42分24秒','%Y年%m月%d日星期六%H时%M分%S秒')
    print(time3)
    time4=datetime.strptime('9/30/2017','%m/%d/%Y')
    print(time4)
    time5=datetime.strptime('9/30/2017 8:42:50 ','%m/%d/%Y %H:%M:%S ')
    print(time5)

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

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

    from datetime import datetime
    now=datetime(2017,9,30)
    a1=now.strftime('today is %Y,%m,%d')
    print(a1)
    a2=now.strftime('today is the %w day of this week')
    print(a2)
    a3=now.strftime('today is the %j day of this year')
    print(a3)
    a4=now.strftime('this week is the %W week of this year')
    print(a4)
    a5=now.strftime('today is the %d day of this month')
    print(a5)

    5、今天是2017年9月30日

       今天是这周的第?天

       今天是今年的第?天 

       今周是今年的第?周 

        今天是当月的第?天

    print('今天是',now.strftime('%Y'),'',now.strftime('%m'),'',now.strftime('%d'),'')
    print('今天是这周的第',now.strftime('%w'),'')
    print('今天是今年的第',now.strftime('%j'),'')
    print('今周是今年的第',now.strftime('%W'),'')
    print('今天是当月的第',now.strftime('%d'),'')

  • 相关阅读:
    SZU:B47 Big Integer II
    Plan : 破晓
    C程序设计语言(第二版)习题:第二章
    Linux : fedora 安装 vnc server
    Linux系统编程:客户端-服务器用FIFO进行通信
    Linux系统编程:dup2()重定向
    Vijos: P1046观光旅游
    FLOYD 求最小环
    uva 401.Palindromes
    codeforces Educational Codeforces Round 5 A. Comparing Two Long Integers
  • 原文地址:https://www.cnblogs.com/1244581939cls/p/7614456.html
Copyright © 2011-2022 走看看