473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Calculating future date from input obtained date

3 2Bits
Hi all,

I am a newby in Python and after a bit of bragging to my wife, she challenged me to generate a due date calculator to confirm the due date of our first-born.

So, via
int(input("Date of conception: (enter in YYYYMMDD) "))
I obtain a date like 20201114.

But now I want to calculate the anticipated due date by taking that date and adding 280 days.

I am getting stuck in this, and would appreciate any help!

Thanks!!

Jorrit
Mar 12 '21 #1
3 3322
JorritTjeertes
3 2Bits
Hi all,

after several hours trying and failing I managed in the end. I am sure the code isn't too elegant, but it gave the result!

Happy to hear any suggestions or thoughts!

Jorrit

Expand|Select|Wrap|Line Numbers
  1. print("Due date calculator")
  2.  
  3. pregnancy_type = int(input("""
  4. What type of pregnancy did you have?
  5. 1: Natural
  6. 2: 3-day IVF transfer
  7. 3: 5-day IVF transfer
  8.  
  9. Please choose 1, 2 or 3: """))
  10.  
  11. print("Please enter the starting date of conception")
  12. conception_day = int(input("Day (DD): "))
  13. conception_month = int(input("Month (MM): "))
  14. conception_year = int(input("Year (YYYY): "))
  15.  
  16. #Calculating the date based on the inputs!!
  17. import datetime
  18. start_date = datetime.datetime(conception_year, conception_month, conception_day)
  19.  
  20. # Last period:  280 days
  21. # 3 day IVF:  263 days
  22. # 5 day IVF:  261 days
  23.  
  24. if pregnancy_type == 1:
  25.     pregnancy_length_1 = 280
  26.     future_date_1 = start_date + datetime.timedelta(days=pregnancy_length_1)
  27.     print(f"""
  28. Your due date is approximately: {future_date_1}
  29.  
  30. Congratulations on your pregnancy!""")
  31. elif pregnancy_type == 2:
  32.     pregnancy_length_2 = 263
  33.     future_date_2 = start_date + datetime.timedelta(days=pregnancy_length_2)
  34.     print(f"""
  35. Your due date is approximately: {future_date_2}
  36.  
  37. Congratulations on your pregnancy!""")
  38. elif pregnancy_type == 3:
  39.     pregnancy_length_3 = 261
  40.     future_date_3 = start_date + datetime.timedelta(days=pregnancy_length_3)
  41.     print(f"""
  42. Your due date is approximately: {future_date_3}
  43.  
  44. Congratulations on your pregnancy!""")
  45. else:
  46.     print("Please only choose 1, 2 or 3")
  47.  
Mar 13 '21 #2
Niheel
2,460 Expert Mod 2GB
Congratulations to the soon to be parents!

Just ran the program. It works.

The only other thing you can do is add validation of the input and display errors. Maybe use a function to check input?

Possibly by using something like this:
Expand|Select|Wrap|Line Numbers
  1. def check_user_input(input):
  2.     try:
  3.         # Convert it into integer
  4.         val = int(input)
  5.         print("Day is ", val)
  6.     except ValueError:
  7.         ask_day()
  8.  
  9. def ask_day():
  10.     conception_day = input("Day (DD):")
  11.     check_user_input(conception_day)
  12.  
  13. conception_day = ask_day()
  14.  
Mar 14 '21 #3
JorritTjeertes
3 2Bits
Hi Niheel,

Thanks for the feedback and suggestion!

I also exchanged the code:
{future_date_3}

with:
{future_date_3.strftime('%Y-%m-%d')}

to only show the date and not the time.

Thanks again!

Jorrit
Mar 14 '21 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Grey | last post by:
I want to know that how to valiate date input from ASP.NET? can i use custom validator? if so, how to.. Million Thanks.
7
by: James P. | last post by:
Hello there, In my asp.net page using VB, I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is greater than or equal to the...
1
by: abcabcabc | last post by:
I write an application which can let user define own date format to input, How to convert the date string to date value with end-user defined date format? Example, User Defined Date Format as...
7
by: Richiep | last post by:
I am trying to get a UK format date of dd/mm/yyyy. Why does the following subroutine not return a valid date in a web form? The date returned is #12:00:00 AM# but the date I entered into the...
6
by: rohayre | last post by:
Im a long time java developer and actually have never done anything with java scripting. I'd like to write a short simple script for calculating a date in the future based on today's date and a...
4
by: Toine | last post by:
Hello, I'm new to Python so please bare with me... I need to calculate a date that is exactly 31 days from the current date in YYYY-MM-DD format. I know that date.today() returns the current...
4
by: Nathan Sokalski | last post by:
When determining whether a String can be converted to a DateTime, you can use the IsDate() method. However, I would also like to know whether the string is a date, a time, or both a date and a...
7
by: Candace W | last post by:
I need to calculate a date that is 90 days from a hire date, excluding weekends and holidays. From everything I have seen, I need to create a separate table listing the holidays. However, I cannot...
4
by: jsallen17 | last post by:
Admin:This thread was originally posted as a hijack in Calculating next x anniversary dates. Hello Stewart. I am trying to calculate a date in the past relative to a future date that has been...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.