Calculating how many days are in a month [duplicate] - python

This question already has answers here:
How to get the last day of the month?
(44 answers)
Closed 9 years ago.
I need to calculate the number of days for a given month in python. If a user inputs Feb 2011 the program should be able to tell me that Feb 2011 has 28 days. Could anyone tell me which library I should use to determine the length of a given month?

You should use calendar.monthrange:
>>> from calendar import monthrange
>>> monthrange(2011, 2)
(1, 28)
Just to be clear, monthrange supports leap years as well:
>>> from calendar import monthrange
>>> monthrange(2012, 2)
(2, 29)
As #mikhail-pyrev mentions in a comment:
First number is the weekday of the first day of the month, the second number is the number of days in said month.

Alternative solution:
>>> from datetime import date
>>> (date(2012, 3, 1) - date(2012, 2, 1)).days
29

Just for the sake of academic interest, I did it this way...
(dt.replace(month = dt.month % 12 +1, day = 1)-timedelta(days=1)).day

Related

Django how to get days count of month in specific year [duplicate]

This question already has answers here:
How to get the last day of the month?
(44 answers)
Closed 9 years ago.
I need to calculate the number of days for a given month in python. If a user inputs Feb 2011 the program should be able to tell me that Feb 2011 has 28 days. Could anyone tell me which library I should use to determine the length of a given month?
You should use calendar.monthrange:
>>> from calendar import monthrange
>>> monthrange(2011, 2)
(1, 28)
Just to be clear, monthrange supports leap years as well:
>>> from calendar import monthrange
>>> monthrange(2012, 2)
(2, 29)
As #mikhail-pyrev mentions in a comment:
First number is the weekday of the first day of the month, the second number is the number of days in said month.
Alternative solution:
>>> from datetime import date
>>> (date(2012, 3, 1) - date(2012, 2, 1)).days
29
Just for the sake of academic interest, I did it this way...
(dt.replace(month = dt.month % 12 +1, day = 1)-timedelta(days=1)).day

how to get the first day and last day of current month in python [duplicate]

This question already has answers here:
How to get the last day of the month?
(44 answers)
Closed 6 years ago.
I am writing an SQL query to get the available data between the first date and last date of the current month in python. For this how can I get first and last date of current month.
Note: In the question that already asked in stackoverflow only deals with end date. Also, I want the answer as a date field like 01-03-2016 or 31-03-2016.
how to get the first day and last day of current month in python
There's a function in the standard library calendar.monthrange(year, month):
>>> import calendar
>>> calendar.monthrange(2016, 3)
(1, 31)
Careful, monthrange doesn't return the dates of first and last days, but returns the weekday of the first day of the month, and number of days in month, for the specified year and month.
So to create first and last date objects, use 1 for the first day, and the number of days for the second day:
>>> _, num_days = calendar.monthrange(2016, 3)
>>> first_day = datetime.date(2016, 3, 1)
>>> last_day = datetime.date(2016, 3, num_days)
>>> first_day
datetime.date(2016, 3, 1)
>>> last_day
datetime.date(2016, 3, 31)
Formatting these as strings:
>>> first_day.strftime('%Y-%m-%d')
'2016-03-01'
>>> last_day.strftime('%Y-%m-%d')
'2016-03-31'

get last day of the month from year month in python [duplicate]

This question already has answers here:
How to get the last day of the month?
(44 answers)
Closed 7 years ago.
I need to get first and last day of a month based on the given yearmonth value. I am able to get the first day, how do we get the last day of the month here ( in python) :
from datetime import date
def first_day_of_month(year,month):
return date(year, month, 1)
print "Today: %s" % date.today()
print ("First day of this month: %s" %
first_day_of_month(2015,10))
This gives the output:
Today: 2015-10-26
First day of this month: 2015-10-01
How to fetch the last day of the month? P.s : I do not want to give 31 as the third parameter for date() function. I want to calculate number of days in the month and then pass on that to the function.
Use calendar.monthrange:
from calendar import monthrange
monthrange(2011, 2)
(1, 28)
# Just to be clear, monthrange supports leap years as well:
from calendar import monthrange
monthrange(2012, 2)
(2, 29)
"Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for year, month."

Get a date based on week number (python) [duplicate]

This question already has answers here:
What's the best way to find the inverse of datetime.isocalendar()?
(8 answers)
Closed 8 years ago.
I'm writing a script where the user needs to input a week number and a procedure will be ran based on that. However, I ran into a small issue, I know I can get week numbers via something like this:
>>> a=datetime.datetime.now()
>>> a
datetime.datetime(2015, 1, 22, 15, 51, 57, 820058)
>>> a.isocalendar()[1]
4
But I can't find how to do it backwards. Also, the date I require has to be Sunday of that week at 6:00am. Once I have that datetime element I can just do
begin_date = datetime.datetime.strptime(a, "%Y-%m-%d %H:%M:%S")
To get the format I want. I'm still missing the step to get the date. Any thoughts?
We create an initial datetime of for 2015 (2014-12-28 6:00:00 --1st sunday of 1st week), and a timedelta object of 7 days:
w0 = datetime.datetime(2014,12,28,6)
d7 = datetime.timedelta(7)
Then you can simply add multiples of the timedelta object to the initial date like so (n would be your week number):
w0+(d7*n)
>>> now = datetime.datetime.now()
>>> start = datetime.datetime(2015, 1,1)
>>> (now - start).days // 7
3

Python How to calculate number of days between 2 dates? [duplicate]

This question already has answers here:
How to calculate number of days between two given dates
(15 answers)
Closed 8 years ago.
I'm having trouble doing this in a clean and simple way in python.
What I'd like to do is having a piece of code that calculates the number of days passed between 2 dates.
For example today being the 22nd of december and i want to know how many days i have before the 15th of febuary. There are 55 days difference
(i took this example because it englobes 2 different years an multiple months)
What i made was very messy and dosen't work half the time so i'm kind of embarrassed to show it.
Any help is appreciated.
Thanks in advance
Simpler implementation:
import datetime
d1 = datetime.datetime(2013,12,22)
d2 = datetime.datetime(2014,2,15)
(d2-d1).days
just create an instance of both the dates and substract them - you'll get timedelta object with a given info.
>>> from datetime import date
>>> by = date(2013, 12, 22)
>>> since = date(2014, 2, 15)
>>> res = since - by
>>> res.days
55
some examples with a variables
>>> variables_tuple = (2013, 12, 22)
>>> by = date(*variables_tuple)
>>> by.year
2013
>>> until_year = 2014
>>> until = date(until_year, 2, 15)

Categories

Resources