AnswerBun.com

How to get all dates up to a certain date?

I have a list of dates in python for example

dates = ["01/03/2010", "02/11/2010", "05/12/2010", "21/12/2010"]

in dd/mm/yyyy format. Then I have a cut-off date "30/11/2010", so I want all dates up to "30/11/2010" from dates, so the output would be

["01/03/2010", "02/11/2010"]

I have tried searching for similar questions but they all seem to be iterating through ranges of dates. Is there any way this can be done in Python?

Stack Overflow Asked by Beck on February 18, 2021

1 Answers

One Answer

If you're doing anything with the dates other than immediately printing them out again, you probably want to convert them to date objects.

import datetime as dt

dates = ["01/03/2010", "02/11/2010", "05/12/2010", "21/12/2010"]
parsed_dates = [dt.datetime.strptime(d, "%d/%m/%Y").date() for d in dates]
filtered_dates = [d for d in parsed_dates if d <= dt.date(2010, 11, 30)]
output_dates = [d.strftime("%d/%m/%Y") for d in filtered_dates]
print(", ".join(output_dates))

Answered by sabik on February 18, 2021

Add your own answers!

Related Questions

Find a cell containing a specific date

1  Asked on December 22, 2021

       

Jeykll file to import not found or unreadable: base

1  Asked on December 22, 2021 by kyu96

       

Unable to remove event listener from document

1  Asked on December 22, 2021 by prawn

     

Invalid result adding the number of 1s in a binary number

2  Asked on December 20, 2021 by aworeham

   

How to print the elements of 3 different arrays by the order?

2  Asked on December 20, 2021 by fuzzyfiso

 

Finding string elements of an array in another array

5  Asked on December 20, 2021 by p-emt

     

How to plot several columns on the same line chart in R?

2  Asked on December 20, 2021 by user13953317

     

Is there a way to craft URLs to fill forms faster?

1  Asked on December 20, 2021 by rosalind-goh

   

infinite loop while using useEffect and redux store

0  Asked on December 20, 2021 by susanta

     

Cannot convert textfield into string

1  Asked on December 20, 2021 by anuj-amin

       

Is there multiple ways to cin vector elements?

1  Asked on December 20, 2021 by 2kbummer

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.