开发者

Python datetime format mask

I have the following datetime format to parse into datetime object in py开发者_运维技巧thon:

Wed, 13 Jul 2011 09:11:14 +0200

What format mask should I use for +0200 part? It means +2 hours.

The thing is that I can't even find any man page on the Internet about datetime format masks.


like this?

In [10]: now = datetime.datetime(*time.strptime("Wed, 13 Jul 2011 09:11:14 +0200", "%a, %d %b %Y %H:%M:%S +0200")[0:5])

In [11]: now
Out[11]: datetime.datetime(2011, 7, 13, 9, 11)

See doc.python.org

For everything related to time and dates, I turn to pleac


It really depends fro what you mean by "parsing". What should happen to the timezone information?

If you intend to purely discard it, you can then instantiate a naïve datetime object as suggested by Fredrik in his answer.

If you intend to retain that information you must create an aware datetime object by setting the tzinfo attribute of your datetime instance. This is the relevant documentation.

HTH!


According to documentation you could use %z, but it turns out it's platform dependent and is not working on my machine. Check it out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜