I am trying to subtract one date value from the value of datetime.datetime.today() to calculate how long ago something was. But it complains: The return value from datetime.datetime.today() doesn’t seem to be “timezone aware”, while my other date value is. How do I get a return value from datetime.datetime.today() that is timezone aware? The ideal solution would be for it
Tag: date
User-friendly time format in Python?
Python: I need to show file modification times in the “1 day ago”, “two hours ago”, format. Is there something ready to do that? It should be in English. Answer The code was originally published on a blog post “Python Pretty Date function” (http://evaisse.com/post/93417709/python-pretty-date-function) It is reproduced here as the blog account has been suspended and the page is no
Is there a function to determine which quarter of the year a date is in?
Sure I could write this myself, but before I go reinventing the wheel is there a function that already does this? Answer Given an instance x of datetime.date, (x.month-1)//3 will give you the quarter (0 for first quarter, 1 for second quarter, etc — add 1 if you need to count from 1 instead;-). Originally two answers, multiply upvoted and
How can I parse a time string containing milliseconds in it with python?
I am able to parse strings containing date/time with time.strptime How can I parse a time string that contains milliseconds? Answer Python 2.6 added a new strftime/strptime macro %f. The docs are a bit misleading as they only mention microseconds, but %f actually parses any decimal fraction of seconds with up to 6 digits, meaning it also works for milliseconds