0

I am trying to truncate the current datetime to the nearest quarter of the year. Meaning, since it is 10th June 2020 right now, I would want the date to be shifted to 1st April 2020 (2nd Quarter of the year), since the 2nd Quarter of the year lasts from 1st April to 30th June (every 3 months is 1 quarter in this case). Is there any clean way to do so?

All help is appreciated, thanks all!

jason
  • 562
  • 8
  • 24
  • 2
    [Related](https://stackoverflow.com/questions/32481256/finding-the-year-and-quarter-from-a-datetime-object-in-python). [Related](https://stackoverflow.com/questions/37135699/get-first-date-and-last-date-of-current-quarter-in-python). `quarter = datetime(d.year, d.month // 3 + 1, 1, 0, 0, 0)` – Olvin Roght Jun 10 '20 at 11:51
  • Does this answer your question? [Is there a Python function to determine which quarter of the year a date is in?](https://stackoverflow.com/questions/1406131/is-there-a-python-function-to-determine-which-quarter-of-the-year-a-date-is-in) – Kassym Dorsel Jun 10 '20 at 11:58
  • in those cases, the returned value is a manipulated form of the datetime object to return whether it is quarter 1, 2, 3 or 4. but what I would want is for it to return the first date of that specific quarter the datetime object is in. is there any clean way to achieve that? – jason Jun 10 '20 at 12:24

1 Answers1

0

I hope this helps!

Using an offset day count parameter you can do

from datetime import datetime, timedelta

# you predefine you offset days difference like below
days_offset = -70

# and caliculate the expected date like below
new_date = datetime.today() + timedelta(days=days_offset)

print(new_date)

Calculating offset days with code

# you can also caliculate the diference like below
diff = datetime.strptime("1 April 2020", '%d %B %Y') - datetime.strptime("10 June 2020", '%d %B %Y')

print(diff.days) # gives -70

Please explore the suggested comments!

sam
  • 1,819
  • 1
  • 18
  • 30
  • thank you for answering, however, I am looking to return the datetime object of the first date of the specific quarter the original datetime object was in. – jason Jun 10 '20 at 12:25
  • yes, but wouldn't that return the quarter of the year in terms of quarter 1, 2, 3, 4? I would want it to be returned as the first date of that specific quarter eg if the date is 10 june, i would want the returned date to be 1 april, not quarter 2 – jason Jun 10 '20 at 12:37
  • @jason, it's so easy to copy one line of code and execute it but you decided to ask me instead? – Olvin Roght Jun 10 '20 at 12:37
  • i am asking you if would perhaps know how to get the output i want to get, since i used the line of code you provided and it did not work. i got the output as 2020-03-01 00:00:00 when what I want is 2020-04-01 (which is the first date of the second quarter) – jason Jun 10 '20 at 12:42
  • 1
    @jason, you have to analyze code, not just copy it and complain that it returns a bit different result. – Olvin Roght Jun 10 '20 at 12:50
  • so if you provided me with a code that does not help me at all, im supposed to just take it and work on it from step 1 again? in that case then what is the point of this platform? well nevermind i'll go figure it out myself then since the code you provided does not help me in any way and you do not seem to be up for a discussion on how to get the desired output, but i guess thanks anyway – jason Jun 10 '20 at 12:53