Days in month as date objects [duplicate] - python

This question already has answers here:
Generate a list of datetimes between an interval
(5 answers)
Closed 7 years ago.
How to generate a list of date objects for each day in a specific month and year.
I've tried using calendar module, but it's not what I want.

Using the timedelta module you can loop through all the possible values nicely and stop when the month changes.
from datetime import date, timedelta
def generate_month_days(year, month):
start_date = date(year, month, 01)
cur_date = start_date
while cur_date.month == start_date.month:
yield cur_date
cur_date += timedelta(days=1)
for d in generate_month_days(2015,11):
print d

Related

TypeError: can only concatenate str (not "relativedelta") to str [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 4 months ago.
i'm trying to get next month from selected date. if i using datetime.date.today() its ok. but i need working with request date. how can i solve it.Thanks
date = request.form['date']
print(date) // 2022-11-05
// nextmonth = datetime.date.today() + relativedelta.relativedelta(months=1)
nextmonth = date + relativedelta.relativedelta(months=1)
You have to convert str to date and then use relativedelta.relativedelta.
Not sure what format the date is, I am assuming it is YYYY-MM-DD.
You can change the day and month format as per what you want.
import datetime
from dateutil import relativedelta
date = '2022-11-05'
date = datetime.datetime.strptime(date, '%Y-%m-%d').date()
nextmonth = date + relativedelta.relativedelta(months=1)
That should give you 2022-12-05.

Extraction of day, month and year from the date 4.5.6

Which function can I use to extract day, month and year from dates written in this manner 4.5.6 where 4 is the day, 5 is the month and 6 is the year (presumably 2006). I have already tried using dateparser.parse but it is not working.
day, month, year = map(int, '4.5.6'.split('.'))
And then add 2000 as necessary to the year.
You can then construct a datetime object with
from datetime import datetime
dt = datetime(year, month, day)
While it would be logical to use datetime.strptime, the one-digit year messes things up, and the above will just work fine.
Here is how you can use the datetime.datetime.strptime() method:
import datetime
s = "4.5.6"
i = s.rindex('.') + 1
s = s[:i] + s[i:].rjust(2, '0') # Add padding to year
dt = datetime.datetime.strptime(s, "%d.%m.%y")
print(dt)
Output:
2006-05-04 00:00:00
With the resulting datetime.datetime object, you can access plenty of information about the date, for example you can get the year by printing dt.year (outputs 2006).

How to calculate the date range of first date and last date of the previous months in python

I am trying to find the first and last date of the previous five months from now using python.
today = datetime.today()
first = today.replace(day=1)
lastMonth = first - timedelta(days=153)
Now how do i find the first and last date of each of the previous five months in python?
Can anyone help me with this please?
The below is based off an two answers already on SO. It adds in the ability to respond with the first and last day over a range of dates.
Get year month for last X months
How to get the first and last day of the month
import calendar
import datetime
from dateutil.relativedelta import relativedelta
def get_last_months(start_date, months):
for i in range(months):
_, num_days = calendar.monthrange(start_date.year,start_date.month)
first_day = datetime.date(start_date.year, start_date.month, 1).strftime('%Y-%m-%d')
last_day = datetime.date(start_date.year, start_date.month, num_days).strftime('%Y-%m-%d')
yield (first_day, last_day)
start_date += relativedelta(months = -1)
months_back = 5
print([i for i in get_last_months(datetime.datetime.today(), months_back)])
Output:
[('2020-10-01', '2020-10-31'), ('2020-09-01', '2020-09-30'), ('2020-08-01', '2020-08-31'), ('2020-07-01', '2020-07-31'), ('2020-06-01', '2020-06-30')]

How to print the next 3 dates from today? [duplicate]

This question already has answers here:
Generating dates to the days of a week in Python?
(3 answers)
Python - Add days to an existing date
(1 answer)
Adding days to a date in Python
(16 answers)
Closed 3 years ago.
I have a code that can tell the weather in entered location.
I want to make an option to print the weather for the next 3 days ,I need to send to my function 3 dates (with a loop), every time different date, how can I send dates of next 3 days from current day?
#This is my function
def weather(city, date):
#This is the part where I send it from the main to the function:
city = 'Paris'
while(i < 4):
i += 1
weather(city.lower(), dd/mm/yyyy)# Here instead of "dd/mm/yyyy" I need to send every time the next date from today.
Use datetime.timedelta(days=1) to increment your day by 1 as follows, you can pass this date to your function
import datetime
curr_date = datetime.datetime.now()
for i in range(4):
curr_date += datetime.timedelta(days=1)
print(curr_date)
#2019-04-19 22:01:29.503352
#2019-04-20 22:01:29.503352
#2019-04-21 22:01:29.503352
#2019-04-22 22:01:29.503352
The simplest way to generate a date range is to use pandas.date_range as
import pandas as pd
dates = pd.date_range('2019-04-10', periods=3, freq='D')
for day in dates:
weather(city, day)
Or you may insist on a loop for next days, you can use datetime.timedelta
from datetime import date, timedelta
one_day_delta = timedelta(1)
day = datetime.date(2019, 4, 19)
for i in range(3)
day += one_day_delta
weather(city, day)

Add 1 day to my date in Python [duplicate]

This question already has answers here:
Adding days to a date in Python
(16 answers)
Closed 2 years ago.
I have the following date format:
year/month/day
In my task, I have to add only 1 day to this date. For example:
date = '2004/03/30'
function(date)
>'2004/03/31'
How can I do this?
You need the datetime module from the standard library. Load the date string via strptime(), use timedelta to add a day, then use strftime() to dump the date back to a string:
>>> from datetime import datetime, timedelta
>>> s = '2004/03/30'
>>> date = datetime.strptime(s, "%Y/%m/%d")
>>> modified_date = date + timedelta(days=1)
>>> datetime.strftime(modified_date, "%Y/%m/%d")
'2004/03/31'

Categories

Resources