Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #13110 > unrolled thread
| Started by | "守株待兔" <1248283536@qq.com> |
|---|---|
| First post | 2011-09-11 12:46 +0800 |
| Last post | 2011-09-12 10:42 -0700 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.python
convert time "守株待兔" <1248283536@qq.com> - 2011-09-11 12:46 +0800
Re: convert time Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-11 15:00 +1000
Re: convert time Ben Finney <ben+python@benfinney.id.au> - 2011-09-11 15:30 +1000
Re: convert time nn <pruebauno@latinmail.com> - 2011-09-12 10:42 -0700
| From | "守株待兔" <1248283536@qq.com> |
|---|---|
| Date | 2011-09-11 12:46 +0800 |
| Subject | convert time |
| Message-ID | <mailman.970.1315716418.27778.python-list@python.org> |
[Multipart message — attachments visible in raw view] — view raw
how can i convert "Dec 11" into 2011-12?
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2011-09-11 15:00 +1000 |
| Message-ID | <4e6c4088$0$29965$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #13110 |
守株待兔 wrote:
> how can i convert "Dec 11" into 2011-12?
if my_str == "Dec 11":
return 1999 # 2011 - 12
Does that help?
But seriously... 2011-12 is not a proper date, so the simplest way is
probably something like this:
def convert(date_str):
month, short_year = date_str.split()
if len(short_year) == 4:
year = short_year
else:
year = '20' + short_year
months = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split()
month = months.index(month) + 1
return year + '-' + str(month)
Otherwise see the time and datetime modules:
http://docs.python.org/library/time.html
http://docs.python.org/library/datetime.html
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2011-09-11 15:30 +1000 |
| Message-ID | <8762kzmys5.fsf@benfinney.id.au> |
| In reply to | #13112 |
Steven D'Aprano <steve+comp.lang.python@pearwood.info> writes:
> But seriously... 2011-12 is not a proper date
It's valid by ISO 8601. The standard allows any number of parts to be
dropped, from least to most significant, in order to have a value with
deliberately reduced precision.
<URL:https://secure.wikimedia.org/wikipedia/en/wiki/ISO_8601#General_principles>
> Otherwise see the time and datetime modules:
>>> import datetime
>>> text = "2011-12"
>>> datetime.datetime.strptime(text, "%Y-%m")
datetime.datetime(2011, 12, 1, 0, 0)
--
\ “It's not what you pay a man, but what he costs you that |
`\ counts.” —Will Rogers |
_o__) |
Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | nn <pruebauno@latinmail.com> |
|---|---|
| Date | 2011-09-12 10:42 -0700 |
| Subject | Re: convert time |
| Message-ID | <d4e6f206-d8ee-4fd4-bb72-c41dbf27cc43@fe21g2000vbb.googlegroups.com> |
| In reply to | #13112 |
On Sep 11, 1:00 am, Steven D'Aprano <steve +comp.lang.pyt...@pearwood.info> wrote: > 守株待兔 wrote: > > how can i convert "Dec 11" into 2011-12? > > if my_str == "Dec 11": > return 1999 # 2011 - 12 > > Does that help? > > But seriously... 2011-12 is not a proper date, so the simplest way is > probably something like this: > > def convert(date_str): > month, short_year = date_str.split() > if len(short_year) == 4: > year = short_year > else: > year = '20' + short_year > months = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split() > month = months.index(month) + 1 > return year + '-' + str(month) > > Otherwise see the time and datetime modules: > > http://docs.python.org/library/time.htmlhttp://docs.python.org/library/datetime.html > > -- > Steven Just a small comment that you can get "months" by doing: from calendar import month_abbr months = list(month_abbr)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web