by dr_zoidberg · 10y ago · 0 comments
In Python:
import datetime # standard module
timedelta = datetime.datetime(2016, 8, 2) - datetime.datetime(2017, 9, 4)
timedelta.days
> -398
timedelta.days / 7
> -57
timedelta.days / 7.
> -56.857142857142854
timedelta.total_seconds() / 3600.
> -9552.0
In particular, that you can pretty much ask anything to Wolfram|Alpha, and that it has to figure out exactly what you're asking (and I admit most of the time, it does it), always seemed more like a marketting ploy than a real feature to me. I can't go as far as saying that I dislike it, but the few times I tried to use it, it didn't get what I was trying to "ask" and I just gave up.--> I also admit it might be me that I just don't get the tool.
For example something like this: https://dateparser.readthedocs.io/en/latest/
Importing packages every time I want to use them is rather tedious though. I guess I need PYTHONSTARTUP as well.
Overall, though, 10 minutes to setup something I only use once or twice a year seems like a waste.
datetime.datetime.strptime("Aug 18 2015", "%b %d %Y")
> datetime.datetime(2015, 8, 18, 0, 0)
Like xapata said, if you import from datetime, it looks "nicer". I prefer to see where things come from most of the time, because I keep open long sessions and it can become messy otherwise.I don't really see how this can take 10 minutes to setup. You can have the import already in a file called dateutils.py and you only set it up once:
import datetime
def string2date(val):
return datetime.datetime.strptime(val, "%b %d %Y")
Then your session becomes easier from dateutils import *
string2date("Jul 4 1776")
datetime.datetime(1776, 7, 4, 0, 0)
However, I think at this point we're talking more about our personal preferences for tools rather than the original subject. It just struck me that your example was something that is available in Python using standard library modules.