Import everyday date for one year

2024/7/7 5:07:41

Is there a simple way to import into my Python file a list with all the dates for a given year? What I need is to store everyday that a year has (01.01.2018----31.12.2018) into a list, so I can then import this list into a column in a table in a SQL database.

I have been Google for hours and didn´t find anything but there should be a way to import this directly...

Anyone knows? Thanks a lot!

Answer

This seems a bit cleaner for what he's looking for and it's a one-liner!

import datetimeyear = 2018
datelist = [d for d in range(datetime.date(year,12,31)-datetime.date(year,1,1)).days)]

This will result in datelist being a list from [1, 2, 3, ... 365/366].

Edit Added Patrick's suggestion and correct formatting as required

All you will need to do to get the format you want would be to get the date from the number explained here and format it correctly. which can also be done in one line but it's a little much for some.

datelist = [datetime.fromordinal(date(year, 1, 1).toordinal() + d - 1).strftime("dd.mm.yyyy") for d in range(1,365+ 1 if isLeap(year) else 0))]

Broken down, here's the long way of the above:

datelist = []
for d in range(1,365+ 1 if isLeap(year) else 0):day = datetime.fromordinal(date(year, 1, 1).toordinal() + d - 1)datelist.append(day.strftime("dd.mm.yyyy"))

This results in a list looking like:

["01.01.2018", "02.01.2018", ... "31.12.2018"]
https://en.xdnf.cn/q/120755.html

Related Q&A

Object detection realtime using tensorflow

Im trying to detect objects in realtime using tensorflow. . I ran jupyter notebook in object_detection directory. then I opened the notebook file. It is firing the following errorIm getting the followi…

How do I get Python to read and extract words from a text file? [duplicate]

This question already has answers here:How to copy files(21 answers)Closed 7 years ago.So I need to make a code that opens a txt file, and then takes the content of that file and puts it into another t…

How to draw a checkered flag to the Python screen?

QUESTION: Implement the following pseudocode to draw a checkered flag to the screen.1. Ask the user for the size of the checkered flag (n). 2. Draw an n x n grid to the screen. 3. For i = 0,2,4,...,…

Open txt file in python

i need to open a txt file . In txt file i have Andrei:Popescu:Bucuresti Maria:Popescu:Targu-Mures ....How do I read a text file into three variable and for each line do something ? Sorry for my englis…

Python 3.3.3 time.sleep() error [duplicate]

This question already has answers here:Python: "global name time is not defined"(8 answers)Closed 10 years ago.Im getting the following error:The error:"Traceback (most recent call last)…

what is the decimal.getcontext().copy() mean [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

how do I change a python interpreter from english to my dialect

I have been stuck for months trying to edit the python interpreter (from www.python.org). All I want to do is to change the keywords eg. change from. English language: print() to Ibo language de().

Python, How to make a Video File(Mpeg/avi etc) into an exe?

First of all, I really dont know the correct direction on where to search or what to search.Going to the point of problems.How can I make my Video File into an exe format?I really need your help guys,…

Is it possible to avoid integers,floats and special characters using try-except statement only?

Im trying to make a code that allows only letters. I know we can do this using isalpha() method. But, Im looking for any other different solutions something like try-except?

Similarity between two text documents in Python

You are provided with four documents, numbered 1 to 4, each with a single sentence of text. Determine the identifier of the document which is the most similar to the first document, as computed accord…