如何将YYYY-MM-DDTHH:mm:ss.000Z时间格式转换为MM / DD / YYYY时间格式? [英] How do you convert YYYY-MM-DDTHH:mm:ss.000Z time format to MM/DD/YYYY time format in Python?

查看:3032
本文介绍了如何将YYYY-MM-DDTHH:mm:ss.000Z时间格式转换为MM / DD / YYYY时间格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我试图将2008-09-26T01:51:42.000Z转换为09/26/2008。最简单的方式是实现这一点?

For example, I'm trying to convert 2008-09-26T01:51:42.000Z to 09/26/2008. What's the simplest way of accomplishing this?

推荐答案

最简单的方法是使用 dateutil .parser.parse()将日期字符串解析为时区感知datetime对象,然后使用strftime()获取所需的格式。

The easiest way is to use dateutil.parser.parse() to parse the date string into a timezone aware datetime object, then use strftime() to get the format you want.

import datetime, dateutil.parser

d = dateutil.parser.parse('2008-09-26T01:51:42.000Z')
print d.strftime('%m/%d/%Y') #==> '09/26/2008'

这篇关于如何将YYYY-MM-DDTHH:mm:ss.000Z时间格式转换为MM / DD / YYYY时间格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆