python GMT时间格式转化

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Kwoky/article/details/84637091

1、datetime类型转换成GMT时间格式的字符串(如'Thu, 19 Feb 2009 16:00:07 GMT'),strftime(官方释义:new string) :

from datetime import datetime

GMT_FORMAT = '%a, %d %b %Y %H:%M:%S GMT+0800 (CST)'

print(datetime.utcnow().strftime(GMT_FORMAT))
 

output:

Mon, 12 Nov 2018 08:53:51 GMT+0800 (CST)

2、将GMT时间格式的字符串转换成datetime类型,strptime(官方释义:new datetime parsed from a string):

dd = "Fri Nov 09 2018 14:41:35 GMT+0800 (CST)"

GMT_FORMAT = '%a %b %d %Y %H:%M:%S GMT+0800 (CST)'

print(datetime.strptime(dd, GMT_FORMAT))
 


output:

2018-11-09 14:41:35

注意:GMT_FORMAT的格式要与索要转化的字符串相对应。
扩展:python的格式转化


%a 本地的星期缩写
%A 本地的星期全称
%b 本地的月份缩写
%B 本地的月份全称
%c 本地的合适的日期和时间表示形式
%d 月份中的第几天,类型为decimal number(10进制数字),范围[01,31]
%f 微秒,类型为decimal number,范围[0,999999],Python 2.6新增
%H 小时(24进制),类型为decimal number,范围[00,23]
%I 小时(12进制),类型为decimal number,范围[01,12]
%j 一年中的第几天,类型为decimal number,范围[001,366]
%m 月份,类型为decimal number,范围[01,12]
%M 分钟,类型为decimal number,范围[00,59]
%p 本地的上午或下午的表示(AM或PM),只当设置为%I(12进制)时才有效
%S 秒钟,类型为decimal number,范围[00,61](60和61是为了处理闰秒)
%U 一年中的第几周(以星期日为一周的开始),类型为decimal number,范围[00,53]。在度过新年时,直到一周的全部7天都在该年中时,才计算为第0周。只当指定了年份才有效。
%w 星期,类型为decimal number,范围[0,6],0为星期日
%W 一年中的第几周(以星期一为一周的开始),类型为decimal number,范围[00,53]。在度过新年时,直到一周的全部7天都在该年中时,才计算为第0周。只当指定了年份才有效。
%x 本地的合适的日期表示形式
%X 本地的合适的时间表示形式
%y 去掉世纪的年份数,类型为decimal number,范围[00,99]
%Y 带有世纪的年份数,类型为decimal number
%Z 时区名字(不存在时区时为空)
%% 代表转义的"%"字符


 

猜你喜欢

转载自blog.csdn.net/Kwoky/article/details/84637091