Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 351879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:41:06+00:00 2026-05-12T11:41:06+00:00

Sure I could write this myself, but before I go reinventing the wheel is

  • 0

Sure I could write this myself, but before I go reinventing the wheel is there a function that already does this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T11:41:06+00:00Added an answer on May 12, 2026 at 11:41 am

    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 even originally accepted (both currently deleted), were buggy — not doing the -1 before the division, and dividing by 4 instead of 3. Since .month goes 1 to 12, it’s easy to check for yourself what formula is right:

    for m in range(1, 13):
      print m//4 + 1,
    print
    

    gives 1 1 1 2 2 2 2 3 3 3 3 4 — two four-month quarters and a single-month one (eep).

    for m in range(1, 13):
      print (m-1)//3 + 1,
    print
    

    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;-) datetools module 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;-).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure this question is appropriate here but I hope I could get
I'm not sure how or why this happened, but if anyone could suggest a
I'm sure there must be something like this somewhere but I can't seem to
Is there a function out there to make sure that any given array conforms
The code I have below works fine but I'm sure it could be more
I am not sure what I could be doing wrong that causes info level
Not sure whats going on here, or what could be the integer in this
I'm not sure if return *this is the only way we could return an
I'm not sure if this is a trivial questions but in a PHP class:
I have in mind a design problem that does not look like complex, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.