Python Forum
Calendar calculations - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Calendar calculations (/thread-14003.html)

Pages: 1 2


Calendar calculations - frequency - Nov-10-2018

Hello,
Do you have any idea how can i start making a program that will print the following day given from a user input? For example

Input 09-11-2001 output 10-11-2001
30-09-2001 01-10-2001
31-12-1999 01-01-2000
The numbers from the day and month will always be double digit form. I have thought of making 3 lists and appending the data to those lists specifially with ":".

Any ideas?


RE: Calendar calculations - stullis - Nov-10-2018

Checkout the datetime module. It's in the standard library. Its Datetime class has a method for converting a string into a date and outputting a new string with custom formatting.


RE: Calendar calculations - ichabod801 - Nov-10-2018

If you are not allowed to use the datetime module for this assignment, you can use .split('-') to get the three parts of the date, and int() to turn those parts into integers. You'll need some conditionals to handle the last day of the month, and the .format() of strings to get the leading 0 on the output (details here).


RE: Calendar calculations - frequency - Nov-11-2018

And then?


RE: Calendar calculations - ichabod801 - Nov-11-2018

And then you write some code. If it doesn't work, show it to us (in python tags), and tell us exactly how it doesn't work.


RE: Calendar calculations - frequency - Nov-12-2018

from datetime import datetime
from dateutil.relativedelta import relativedelta

print ('Today:',datetime.now().strftime('%d/%m/%Y %H:%M:%S'))
date_after_month = datetime.now()+ relativedelta(days=5)
print ('After 5 Days:', date_after_month.strftime('%d/%m/%Y %H:%M:%S'))
This code calculates given days from the CURRENT date. Is there any option to manipulate datetime.now() in order to replace it with a users input? Thanks!


RE: Calendar calculations - stullis - Nov-12-2018

The datetime class has a method called strptime() which enables you to change a string into a date. For example:

inputted_date = "12/10/2017"
example_date = datetime.datetime.strptime(inputted_date, "%m/%d/%Y")



RE: Calendar calculations - frequency - Nov-12-2018

from datetime import datetime
from dateutil.relativedelta import relativedelta
inputted_date=input()

while inputted_date!="end" and inputted_date!="END":
  example_date = datetime.strptime(inputted_date, "%d-%m-%Y")
  #print ('Today:',datetime.now().strftime('%d/%m/%Y %H:%M:%S'))
  tommorow = example_date+ relativedelta(days=1)
  print ('After 1 Days:', tommorow.strftime('%d-%m-%Y'))
  inputted_date=input()
Modified it to work on repl.it BUT, on python's idle i get this error.
Traceback (most recent call last):
  File "C:/Users/FREQUENCY/Desktop/test.py", line 2, in <module>
    from dateutil.relativedelta import relativedelta
ImportError: No module named dateutil.relativedelta
>>> 



RE: Calendar calculations - nilamo - Nov-12-2018

(Nov-12-2018, 07:25 PM)frequency Wrote: ImportError: No module named dateutil.relativedelta
If you want to use third party libs, you need to install them first: pip install python-dateutil. https://github.com/dateutil/dateutil/


RE: Calendar calculations - frequency - Nov-12-2018

Yes,you are correct. The funny thing is,that i cant submit my code to my code editor in my university since it is missing the 3party libs. Hm. i must redo the code