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'),'')

  • 相关阅读:
    Leetcode题目:House Robber II
    Leetcode题目:Compare Version Numbers
    Leetcode题目:Intersection of Two Arrays II
    Leetcode题目:Intersection of Two Arrays
    Mac OS X 好用的软件包管理工具 Homebrew
    Linux 安装配置 JDK 8
    Centos 6.5 RedHat 6 安装mysql
    Ubuntu 源
    grub2 使用memdisk工具 启动任意iso
    Fedora 21 设置开机启动脚本
  • 原文地址:https://www.cnblogs.com/1244581939cls/p/7614456.html
Copyright © 2011-2022 走看看