Sure I could write this myself, but before I go reinventing the wheel is there a function that already does this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Given an instance
xof datetime.date,(x.month-1)//3will 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 even originally accepted (both currently deleted), were buggy — not doing the
-1before the division, and dividing by 4 instead of 3. Since.monthgoes 1 to 12, it’s easy to check for yourself what formula is right:gives
1 1 1 2 2 2 2 3 3 3 3 4— two four-month quarters and a single-month one (eep).gives
1 1 1 2 2 2 3 3 3 4 4 4— now doesn’t this look vastly preferable to you?-)This proves that the question is well warranted, I think;-).
I don’t think the datetime module should necessarily have every possible useful calendric function, but I do know I maintain a (well-tested;-)
datetoolsmodule for the use of my (and others’) projects at work, which has many little functions to perform all of these calendric computations — some are complex, some simple, but there’s no reason to do the work over and over (even simple work) or risk bugs in such computations;-).