Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3.2 input date function

I would like to write a function that takes a date entered by the user, stores it with the shelve function and prints the date thirty days later when called.

I'm trying to start with something simple like:

import datetime

def getdate():
    date1 = input(datetime.date)
    return date1

getdate()

print(date1)

This obviously doesn't work.

I've used the answers to the above question and now have that section of my program working! Thanks! Now for the next part:

I'm trying to write a simple program that takes the date the way you instructed me to get it and adds 30 days.

import datetime
from datetime import timedelta

d = datetime.date(2013, 1, 1)
print(d)
year, month, day = map(int, d.split('-'))
d = datetime.date(year, month, day)
d = dplanted.strftime('%m/%d/%Y')
d = datetime.date(d)+timedelta(days=30)
print(d)

This gives me an error: year, month, day = map(int, d.split('-')) AttributeError: 'datetime.date' object has no attribute 'split'

Ultimately what I want is have 01/01/2013 + 30 days and print 01/30/2013.

Thanks in advance!

like image 364
Gregory6106 Avatar asked Mar 05 '13 14:03

Gregory6106


People also ask

How do I input a date in Python?

To create a date from user input:Use the input() function to take a value formatted as YYYY-MM-DD . Split the input value on the hyphens and convert the date components to integers. Use the date() class from the datetime module to create the date.

How do I print a date in YYYY-MM-DD in Python?

strftime() Function to Format Date to YYYYMMDD in Python. The strftime() is used to convert a datetime object to a string with a given format. We can specify the format for YYYY-MM-DD in the function as %Y-%m-%d . Note that this function returns the date as a string.

What does date () do in Python?

The date() instance method of the python datetime class returns a date instance. Using this method only the date information excluding the time information is retrieved from a datetime instance.

How do I get the current date in Python 3?

today() The today() method of date class under DateTime module returns a date object which contains the value of Today's date. Returns: Return the current local date.


2 Answers

Thanks. I have been trying to figure out how to add info to datetime.datetime(xxx) and this explains it nicely. It's as follows

datetime.datetime(year,month, day, hour, minute, second) with parameters all integer. It works!

like image 124
Arnold Avatar answered Nov 10 '22 11:11

Arnold


The input() method can only take text from the terminal. You'll thus have to figure out a way to parse that text and turn it into a date.

You could go about that in two different ways:

  • Ask the user to enter the 3 parts of a date separately, so call input() three times, turn the results into integers, and build a date:

    year = int(input('Enter a year'))
    month = int(input('Enter a month'))
    day = int(input('Enter a day'))
    date1 = datetime.date(year, month, day)
    
  • Ask the user to enter the date in a specific format, then turn that format into the three numbers for year, month and day:

    date_entry = input('Enter a date in YYYY-MM-DD format')
    year, month, day = map(int, date_entry.split('-'))
    date1 = datetime.date(year, month, day)
    

Both these approaches are examples; no error handling has been included for example, you'll need to read up on Python exception handling to figure that out for yourself. :-)

like image 40
Martijn Pieters Avatar answered Nov 10 '22 11:11

Martijn Pieters