本文介绍了将日期字符串转换为星期几的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的日期字符串:

 '2010年1月11日'

,我需要一个返回星期几的函数,如

 'mon'或'monday'

等。
我在Python帮助中找不到任何地方。
任何人?谢谢。

解决方案

你可能想使用方法从:

 >>> import datetime 
>>>> datetime.datetime.strptime('2010年1月11日''%B%d,%Y')strftime('%A')
'星期一'
/ pre>

'Mon'

 >>> datetime.datetime.strptime('2010年1月11日''%B%d,%Y')strftime('%a')
'Mon'
/ pre>

I have date strings like this:

'January 11, 2010'

and I need a function that returns the day of the week, like

'mon', or 'monday'

etc.I can't find this anywhere in the Python help.Anyone? Thanks.

解决方案

You might want to use strptime and strftime methods from datetime:

>>> import datetime
>>> datetime.datetime.strptime('January 11, 2010', '%B %d, %Y').strftime('%A')
'Monday'

or for 'Mon':

>>> datetime.datetime.strptime('January 11, 2010', '%B %d, %Y').strftime('%a')
'Mon'

这篇关于将日期字符串转换为星期几的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 09:31