👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
I"m using:
str(datetime.datetime.today()).split()[0]
to return today"s date in the YYYY-MM-DD
format.
Is there a less crude way to achieve this?
👻 Read also: what is the best laptop for engineering students?
How do I calculate the date six months from the current date using the datetime Python module?
1 answers
I am using the datetime Python module. I am looking to calculate the date 6 months from the current date. Could someone give me a little help doing this?
The reason I want to generate a date 6 months from the current date is to produce a review date. If the user enters data into the system it will have a review date of 6 months from the date they entered the data.
Answer #1
I found this solution to be good. (This uses the python-dateutil extension)
from datetime import date
from dateutil.relativedelta import relativedelta
six_months = date.today() + relativedelta(months=+6)
The advantage of this approach is that it takes care of issues with 28, 30, 31 days etc. This becomes very useful in handling business rules and scenarios (say invoice generation etc.)
$ date(2010,12,31)+relativedelta(months=+1)
datetime.date(2011, 1, 31)
$ date(2010,12,31)+relativedelta(months=+2)
datetime.date(2011, 2, 28)
We hope this article has helped you to resolve the problem. Apart from Getting today”s date in YYYY-MM-DD in Python?, check other code Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Getting today”s date in YYYY-MM-DD in Python?
- Deutsch Getting today”s date in YYYY-MM-DD in Python?
- Français Getting today”s date in YYYY-MM-DD in Python?
- Español Getting today”s date in YYYY-MM-DD in Python?
- Türk Getting today”s date in YYYY-MM-DD in Python?
- Русский Getting today”s date in YYYY-MM-DD in Python?
- Português Getting today”s date in YYYY-MM-DD in Python?
- Polski Getting today”s date in YYYY-MM-DD in Python?
- Nederlandse Getting today”s date in YYYY-MM-DD in Python?
- 中文 Getting today”s date in YYYY-MM-DD in Python?
- 한국어 Getting today”s date in YYYY-MM-DD in Python?
- 日本語 Getting today”s date in YYYY-MM-DD in Python?
- हिन्दी Getting today”s date in YYYY-MM-DD in Python?
Texas | 2023-03-29
Maybe there are another answers? What Getting today”s date in YYYY-MM-DD in Python? exactly means?. I am just not quite sure it is the best method
Moscow | 2023-03-29
Thanks for explaining! I was stuck with Getting today”s date in YYYY-MM-DD in Python? for some hours, finally got it done 🤗. I just hope that will not emerge anymore
Milan | 2023-03-29
Thanks for explaining! I was stuck with Getting today”s date in YYYY-MM-DD in Python? for some hours, finally got it done 🤗. Will get back tomorrow with feedback