【Python】python 日期、时间、字符串相互转换

【个人笔记】

(1) 区分:strptime、strftime

a="04/12/2014 06:08:55"

a1 = datetime.datetime.strptime(a,"%d/%m/%Y %H:%M:%S")    # 将字符串a转化为datetime,用的是strptime

a2 = datetime.datetime.strftime(a1,'%Y-%m-%d %H:%M:%S')      # 将datetime b格式化 ,用的是strftime

b = 'Wed May 01 01:04:20 +0000 2019'

b1 = datetime.datetime.strptime(b,"%a %b %d %H:%M:%S +0000 %Y")

b2 = datetime.datetime.strftime(b1,'%Y-%m-%d %H:%M:%S')

另外dateTime类型和date类型可以直接做加1减1这种操作

#!/usr/bin/env python3
import datetime
# today = datetime.datetime.today()
today = datetime.datetime.today().date()
yestoday = today + datetime.timedelta(days=-1)
tomorrow = today + datetime.timedelta(days=1)
print(today) # 2019-01-30
print(yestoday)# 2019-01-29
print(tomorrow)# 2019-01-31

格式化字符串的意义:

%a 星期的简写。如 星期三为Web
%A 星期的全写。如 星期三为Wednesday
%b 月份的简写。如4月份为Apr
%B月份的全写。如4月份为April 
%c:  日期时间的字符串表示。(如: 04/07/10 10:43:39)
%d:  日在这个月中的天数(是这个月的第几天)
%f:  微秒(范围[0,999999])
%H:  小时(24小时制,[0, 23])
%I:  小时(12小时制,[0, 11])
%j:  日在年中的天数 [001,366](是当年的第几天)
%m:  月份([01,12])
%M:  分钟([00,59])
%p:  AM或者PM
%S:  秒(范围为[00,61],为什么不是[00, 59],参考python手册~_~)
%U:  周在当年的周数当年的第几周),星期天作为周的第一天
%w:  今天在这周的天数,范围为[0, 6],6表示星期天
%W:  周在当年的周数(是当年的第几周),星期一作为周的第一天
%x:  日期字符串(如:04/07/10)
%X:  时间字符串(如:10:43:39)
%y:  2个数字表示的年份
%Y:  4个数字表示的年份
%z:  与utc时间的间隔 (如果是本地时间,返回空字符串)
%Z:  时区名称(如果是本地时间,返回空字符串)
%%:  %% => %

 

(2)认识

b = datetime.datetime.strptime('2019-05-10 00:00:55','%Y-%m-%d %H:%M:%S')

e=datetime.datetime.now() -b                           # 时间减法

print(type(e), e)                                                 #  1554 days, 16:00:33.813869

print(e.days, e.seconds, e.microseconds)        # 1554 57944 703557

说明:class datetime.timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]]),内部只存储days,seconds,microseconds,    

(3)datetime与时间戳的相互转化

import datetime, time
def unix_time():
    #将python的datetime转换为unix时间戳
    dtime = datetime.datetime.now()
    un_time = time.mktime(dtime.timetuple())
    print un_time  #1509636609.0
    #将unix时间戳转换为python  的datetime
    unix_ts = 1509636585.0
    times = datetime.datetime.fromtimestamp(unix_ts)
    print times  #2017-11-02 23:29:45

 ---------------------------------------------------------------------------------------------------

在python中,日期类型date和日期时间类型dateTime是不能比较的。

(1)如果要比较,可以将dateTime转换为date,date不能直接转换为dateTime

import datetime
dateTime_p = datetime.datetime.now()  
date_p = dateTime_p.date() 
print(dateTime_p) #2019-01-30 15:17:46.573139
print(date_p) #2019-01-30

 

(2)日期类型date转换为字符串str

#!/usr/bin/env python3
import datetime
date_p = datetime.datetime.now().date()
str_p = str(date_p)
print(date_p,type(date_p)) #2019-01-30 
print(str_p,type(str_p)) #2019-01-30 

(3)字符串类型str转换为dateTime类型

import datetime
str_p = '2019-01-30 15:29:08'
dateTime_p = datetime.datetime.strptime(str_p,'%Y-%m-%d %H:%M:%S')
print(dateTime_p) # 2019-01-30 15:29:08

print(now.strftime('%a, %b %d %H:%M'))

Mon, May 08 20:22

%a,星期几,缩写

%b,月份,缩写

%d,第几日,罗马数字

%Y-%m-%d %H:%M:%S,年-月-日 时-分-秒,罗马数字

(4)dateTime类型转为str类型

  这个地方我也不太理解,为什么指定格式无效

import datetime
dateTime_p = datetime.datetime.now()
str_p = datetime.datetime.strftime(dateTime_p,'%Y-%m-%d')
print(dateTime_p) # 2019-01-30 15:36:19.415157

(5)字符串类型str转换为date类型

#!/usr/bin/env python3
import datetime
str_p = '2019-01-30'
date_p = datetime.datetime.strptime(str_p,'%Y-%m-%d').date()
print(date_p,type(date_p)) # 2019-01-30 

 

你可能感兴趣的:(Python编程手册)