4 ms·
>> The part I've found most useful has been Wolfram|Alpha; it's really nice to be able to say "August 2, 2016 - September 4, 2017" and get back the number of da
by dr_zoidberg 10y ago
>> The part I've found most useful has been Wolfram|Alpha; it's really nice to be able to say "August 2, 2016 - September 4, 2017" and get back the number of days, weeks, and hours between those two times.
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.
- Mathnerd314 10y agoAdd a parser (so I can say "August" or "Aug" instead of 8) and I might start using it (just because the Wolfram|Alpha page loads so slowly). For example something like this: https://dateparser.readthedocs.io/en/latest/ 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.
- dr_zoidberg 10y agoNo need to, already included (though I'm not so familiar with the datetime module, so I had to look for it): 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.
- dr_zoidberg 10y agoA coworker noted that dateparser[0] is a strong module for this use case. [0] https://pypi.python.org/pypi/dateparser https://pypi.python.org/pypi/dateparser
- zump 10y agoYuck.
- xapata 10y agoIt looks a little nicer if you say from datetime import datetime, timedelta But the code already looked pretty good to me.