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?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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 dateutil.parser d = dateutil.parser.parse('2008-09-26T01:51:42.000Z') print(d.strftime('%m/%d/%Y')) #==> '09/26/2008'
回答2:
>>> import time >>> timestamp = "2008-09-26T01:51:42.000Z" >>> ts = time.strptime(timestamp[:19], "%Y-%m-%dT%H:%M:%S") >>> time.strftime("%m/%d/%Y", ts) '09/26/2008'
See the documentation of the Python time
module for more information.
回答3:
I know this is really old question, but you can do it with python datetime.strptime()
>>> from datetime import datetime >>> date_format = "%Y-%m-%dT%H:%M:%S.%fZ" >>> datetime.strptime('2008-09-26T01:51:42.000Z', date_format) datetime.datetime(2008, 9, 26, 1, 51, 42)
回答4:
2008-09-26T01:51:42.000Z
is an ISO8601 date and the format can be very diverse. If you want to parse these dates see the python wiki on working with time. It contains some useful links to modules.
回答5:
def datechange(datestr): dateobj=datestr.split('-') y=dateobj[0] m=dateobj[1] d=dateobj[2] datestr=d +'-'+ m +'-'+y return datestr
U can make a function like this which take date object andd returns you date in desired dateFormat....