码迷,mamicode.com
首页 > 其他好文 > 详细

datetime处理日期和时间

时间:2017-09-30 15:00:38      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:one   技术   format   国际   变量   ofo   class   输出   分享   

  • 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

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

      • 今天是2017年9月30日
        今天是这周的第?天 
        今天是今年的第?天 
        今周是今年的第?周 
        今天是当月的第?天from datetime import datetime

 

from datetime import datetime
#课程内容:
date=datetime(2017, 5, 23, 12, 20) 
print(创建:,date)
now=datetime.now()
print(现在的时间:,now)
b=datetime.utcnow()
print(国际标准时间:,b)
c=datetime(2017,5,23,12,20)
print(c)
d=datetime.strptime(2015-6-1 18:19:59,%Y-%m-%d %H:%M:%S)
print(转换:,d)
n=datetime.now()
print(type(n))
print(现在时间类型:,n)
dt=datetime(2016,3,8,12,12)
print(dt)
e=now.microsecond
print(现在时间微秒:,e)
f=now.isoweekday()
print(星期几:,f)
g=now.isoformat()
print(格式:,g)
#作业1:字符串转换成datetime类型
cday1=datetime.strptime(2017/9/30,%Y/%m/%d)
print(day1=,cday1)
cday2=datetime.strptime(2017年9月30日星期六,%Y年%m月%d日星期六)
print(day2=,cday2)
cday3=datetime.strptime(2017年9月30日星期六8时42分24秒,%Y年%m月%d日星期六%H时%M分%S秒)
print(day3=,cday3)
cday4=datetime.strptime(9/30/2017,%m/%d/%Y)
print(day4=,cday4)
cday5=datetime.strptime(9/30/2017 8:42:50,%m/%d/%Y %H:%M:%S)
print(day5=,cday5)
#作业2:datetime类型转换字符串类型
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))
#作业3:用datetime类型的变量输出以下字符串
print(d.strftime(今天是%Y年%m月%d日))
print(d.strftime(今天是这周的第%w天))
print(d.strftime(今天是今年的第%j天))
print(d.strftime(今周是今年的第%W周))
print(d.strftime(今年是当月的第%d天))

技术分享

datetime处理日期和时间

标签:one   技术   format   国际   变量   ofo   class   输出   分享   

原文地址:http://www.cnblogs.com/jzx-089/p/7614455.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!