calculating the next day from a "YYYYMMDD" formatted string
Asked Answered
M

3

9

How can I calculate the next day from a string like 20110531 in the same YYYYMMDD format? In this particular case, I like to have 20110601 as the result. Calculating "tomorrow" or next day in static way is not that tough, like this:

>>> from datetime import date, timedelta
>>> (date.today() + timedelta(1)).strftime('%Y%m%d')
'20110512'
>>>
>>> (date(2011,05,31) + timedelta(1)).strftime('%Y%m%d')
'20110601'

But how can I use a string like dt = "20110531" to get the same result as above?

Maples answered 11/5, 2011 at 11:59 Comment(0)
C
12

Here is an example of how to do it:

import time
from datetime import date, timedelta

t=time.strptime('20110531','%Y%m%d')
newdate=date(t.tm_year,t.tm_mon,t.tm_mday)+timedelta(1)
print newdate.strftime('%Y%m%d')
Czarina answered 11/5, 2011 at 12:18 Comment(6)
I think you can just say t.date() instead of pulling the three attributes off explicitly. But, +1 for noticing that the rest of us were giving him datetime objects when he might have just wanted a date!Factory
So, what's the advantage of using date over datetime? I far as I can see, they are doing the very same thing taking the same amount of time. Cheers!!Maples
You used date in your original question, so I used it in my answer. The question of which to use depends only on precision: do you need to know the time, or just the date?Czarina
Get it now, date is just fine in this case. Thanks for the script, that's the on;y thing works with v2.4 so far. Cheers!!Maples
@BrandonRhodes No t.date() at least in Python 2.7.Helical
You might want to check whether your Python was compiled with all of the official modules available? The Python documentation claims that the datetime type's date() method has been available since the datetime module itself was added in Python 2.3: docs.python.org/2/library/datetime.html#datetime.datetime.dateFactory
F
4
>>> from datetime import datetime
>>> print datetime.strptime('20110531', '%Y%m%d')
2011-05-31 00:00:00

And then do math on that date object as you show in your question.

The datetime library docs.

Factory answered 11/5, 2011 at 12:3 Comment(3)
Many thanks, that's just the thing I was looking for. I feel myself so stupid though now. Cheers!!Maples
I'm sorry to say it's actually not working in v2.4, where actually I need to run this script. it says: AttributeError: type object 'datetime.datetime' has no attribute 'strptime'. So I'm back to the base again.Maples
Yes, AJ's answer works, only if I put theose three attributes explicitly. How to use t.date() instead? Cheers!!Maples
B
3

You are most of the way there! along with the strftime function which converts a date to a formatted string, there is also a strptime function which converts back the other way.

To solve your problem you can just replace date.today() with strptime(yourDateString, '%Y%m%d').

ED: and of course you will also have to add strptime to the end of your from datetime import line.

Bowers answered 11/5, 2011 at 12:3 Comment(2)
I see it works just fine without adding strptime. I had a look in the strptime() but didn't understand the use properly. Thanks for the comment. Cheers!!Maples
I see your point now - it's a v2.6 vs. v2.4 issue. In v2.6, no need to import strptime. In 2.4 it reports error: ImportError: cannot import name strptime but datetime.strptime() doesn't work either.Maples

© 2022 - 2024 — McMap. All rights reserved.