Add 1 day to my date in Python [duplicate] - python

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'

Related

Convert number string to weekday [duplicate]

This question already has answers here:
Converting date/time in YYYYMMDD/HHMMSS format to Python datetime
(3 answers)
How do I get the day of week given a date?
(30 answers)
Closed last month.
How do I convert 20230102 to Monday?
Using python, I need to accomplish this. I have a column of numbers in the format yyyymmdd.
Parse with strptime and format with strftime:
>>> from datetime import datetime
>>> n = 20230102
>>> datetime.strptime(str(n), "%Y%m%d").strftime("%A")
'Monday'
See strftime() and strptime() Format Codes for documentation of the % strings.
You can convert number string into weekday using "datetime" module
import datetime
def get_weekday(date):
date = datetime.datetime.strptime(date, '%Y%m%d')
return date.strftime('%A')
print(get_weekday('20230102'))
This is how you can achieve your desired output.
You can do it with weekday() method.
from datetime import date import calendar
my_date = date.today()
calendar.day_name[my_date.weekday()] #Friday

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.

Re-formatting a date in python [duplicate]

This question already has answers here:
Convert String with month name to datetime
(2 answers)
Closed 6 months ago.
So I have a date in this format "August 10, 2022". I need to reformat the date in this way "2022-08-10". How do I do that in python ?.
For this, you can use datetime, specifically on this behaviour.
from datetime import datetime
a = "August 10, 2022"
b = datetime.strptime(a, '%B %d, %Y') # Converts to datetime format
c = b.strftime('%Y-%m-%d') # Converts from datetime to desired format
print(c)
# output
2022-08-10

How to convert time to T Z format in python [duplicate]

This question already has answers here:
Generate RFC 3339 timestamp in Python [duplicate]
(7 answers)
Closed 2 years ago.
I would like to convert today's date to below format in python
What I tried:
>>> import datetime
>>> d_date = datetime.datetime.now()
>>> reg_format_date = d_date.strftime("%Y-%m-%d %I:%M:%S %p")
>>> print(reg_format_date)
2020-08-04 06:40:52 PM
Expected format:
2017-10-18T04:46:53.553472514Z
can some one suggest please
Use utcnow() instead of now() to get the UTC time.
Use the isoformat() method. You'll need to add the trailing "Z" yourself.
In summary:
from datetime import datetime
reg_format_date = datetime.utcnow().isoformat() + "Z"
Here's how to get the current UTC time and convert to the ISO-8601 format (which is what your example shows). The timezone is hardcoded to Z.
import datetime
datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None).isoformat() + 'Z'

Python: Convert string into date format [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 7 years ago.
I know this has been asked a few times, but my scenario is a little different... The objective I need to accomplish is to convert a string of digits '20150425' (which happens to be a date), into a date format such as, '2015-04-25'. I need this because I am trying to compare date objects in my code, but have one variable type represented as a string.
Example below:
date = '20150425' ## want to convert this string to date type format
# conversion here
conv_date = '2015-04-25' ## format i want it converted into
Hope this is clear. Should not be difficult, just do not know how to do it.
This works
from datetime import datetime
date = '20150425'
date_object = datetime.strptime(date, '%Y%m%d')
date_object
>>> datetime.datetime(2015,4,25,0,0)
Assuming the date strings will always be 8 characters:
date = '20150425'
fdate = "{}-{}-{}".format(date[0:4], date[4:6], date[6:]) # 2015-04-25
Alternatively, you can go the "heavier" route and use the actual datetime class:
from datetime import datetime
date = '20150425'
dt = datetime.strptime(date, "%Y%m%d")
dt.strftime("%Y-%m-%d") # 2015-04-25

Categories

Resources