Adding one day at a time from a starting date [duplicate] - python

This question already has answers here:
How to increment a datetime by one day?
(8 answers)
Closed 4 years ago.
Can someone show me a few lines of code on how to add one day to datetime?
Like if you had some initial date:
start_date = datetime.date(1847, 3, 30)
and simply wanted to change it to (1847, 3, 31)
and then (1847, 4, 1)
and so on.
I'm new to Python and just trying to wrap my head around this import.

startdate + datetime.timedelta(days=1)
will give you the answer

Related

Change format of dates in Pandas DF [duplicate]

This question already has answers here:
How to change the datetime format in Pandas
(8 answers)
Closed 12 months ago.
I have a pandas dataframe with dates in the following format:
Dec 11, 2018
Wondering is there an easy way to change the format to 11/12/2018? I know I can go through each month manually but not sure what my next step would be to switch around the month and day and add the /.
Thanks in advance!
Use strftime('%m/%d/%Y'):
s = pd.Series(['Dec 11, 2018'])
pd.to_datetime(s).dt.strftime('%m/%d/%Y')
Output:
0 12/11/2018
dtype: object

How do I ask user for date input in python? [duplicate]

This question already has answers here:
Python 3.2 input date function
(5 answers)
Closed 1 year ago.
I want to make def function for schedules.
Here's what I imagine:
User input: yyyy-mm-dd
Output: the next day after the input, next 2 days, next 4 days
I tried to make the function but it doesn't work, please help. Thank you
This question is already answered here:
Python 3.2 input date function
To add 2 days to the input date you would have to do something like:
date = datetime.date(year, month, day)
new_date = date + timedelta(2)

How can I sort date array [duplicate]

This question already has answers here:
Sort a Python date string list
(2 answers)
Sort list of date strings
(2 answers)
Closed 5 years ago.
Please see my following code snippet
Input
list_x = ["11/1/2100", "5/12/1999", "19/1/2003", "11/9/2001"]
Output
['5/12/1999', '11/9/2001', '19/1/2003', '11/1/2100']
You can convert your day/month format to day in year format and then compare each element in the list based on the year then the day

Subtract one date from another and get int [duplicate]

This question already has answers here:
How to calculate number of days between two given dates
(15 answers)
Closed 7 years ago.
How can I subtract on datefield from another and get result in integer?
Like 11.05.2015-10.05.2015 will return 1
I tried
entry.start_devation= each.start_on_fact - timedelta(days=data.calendar_start)
Take the difference between two dates, which is a timedelta, and ask for the days attribute of that timedelta.

how to find current day is weekday or weekends in Python? [duplicate]

This question already has answers here:
How do I get the day of week given a date?
(30 answers)
Closed 7 years ago.
Please suggest me on the following.
How to find whether a particular day is weekday or weekend in Python?
You can use the .weekday() method of a datetime.date object
import datetime
weekno = datetime.datetime.today().weekday()
if weekno < 5:
print "Weekday"
else: # 5 Sat, 6 Sun
print "Weekend"
Use the date.weekday() method. Digits 0-6 represent the consecutive days of the week, starting from Monday.

Categories

Resources