Python 3.2 input date function

121,223

Solution 1

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. :-)

Solution 2

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!

Solution 3

Use the dateutils module

    from dateutil import parser

    date = parser.parse(input("Enter date: "))
Share:
121,223
Gregory6106
Author by

Gregory6106

Updated on July 09, 2022

Comments

  • Gregory6106
    Gregory6106 almost 2 years

    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!

  • wRAR
    wRAR about 11 years
    dateutil can parse dates in various string formats automatically.
  • Martijn Pieters
    Martijn Pieters about 11 years
    @wRAR: Sure, but that's the next level; the important part is understanding how input() works.
  • Gregory6106
    Gregory6106 about 11 years
    thank you for the guidance, and I do appreciate: "you'll need to read up on Python exception handling to figure that out for yourself. :-)" You're forcing me to learn ;)
  • Gregory6106
    Gregory6106 about 11 years
    On this line, "map(int, date_entry.split('-'))" what does the "map" do? And also, because I have found the split method, (is it a method?), when it says date_entry.split('-'), are you telling it to split the value at each dash? or does it recognize the seperation of year, month, day and create a dash between each value?
  • Martijn Pieters
    Martijn Pieters about 11 years
    @user2136162: see docs.python.org/2/library/stdtypes.html#str.split and docs.python.org/2/library/functions.html#map :-) str.split() returns a tuple with the elements between the split string (so 'abc-def'.split('-') gives you ('abc', 'def'). map(callable, sequence) calls the callable for each element in the sequence, so the strings are mapped to integers.
  • Gregory6106
    Gregory6106 about 11 years
    Ok, back to the books! Thanks again for the help.
  • Arthur Khazbs
    Arthur Khazbs about 4 years
    This is not pythonic, consider using strptime to parse datetime.
  • Martijn Pieters
    Martijn Pieters about 4 years
    @ArthurKhazbs: that depends entirely on your use-cases. There is nothing pythonic or unpythonic about UI design. datetime.strptime() is just one of the options in the arsenal, but not that it is not available on date. So you'd have to use datetime.strptime(date_entry, "%Y-%m-%d").date(), and has different error output for wrong dates. Try to enter 2019-2-29, strptime gives you a very cryptic error message: ValueError: unconverted data remains: 9, whereas the method in my answer gives you ValueError: day is out of range for month.