Python Forum
Calendar calculations
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calendar calculations
#1
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?
Reply
#2
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.
Reply
#3
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).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
And then?
Reply
#5
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
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!
Reply
#7
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")
Reply
#8
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
>>> 
Reply
#9
(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/
Reply
#10
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Task calendar problem humanical 8 1,934 Sep-04-2023, 02:55 PM
Last Post: Pedroski55
  Calendar program louienyy 2 5,078 Mar-30-2020, 01:21 PM
Last Post: louienyy
Sad [Learning] Calendar without modules or list KoFu 5 4,971 Sep-09-2019, 03:25 PM
Last Post: DeaD_EyE
  Weird problem with reading from file and performing calculations pineapple999 1 3,083 Jul-25-2019, 01:30 AM
Last Post: ichabod801
  Convert number to a different base and make calculations frequency 12 5,830 Dec-17-2018, 05:21 PM
Last Post: frequency
  Prime numbers calculations frequency 3 3,058 Nov-27-2018, 07:07 PM
Last Post: micseydel
  dc circuit calculations michaelj 1 2,367 Nov-01-2017, 09:19 AM
Last Post: heiner55
  Calendar calcualtions deusvult 11 8,506 May-01-2017, 02:56 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020