Python 3.2 输入日期函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15226898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python 3.2 input date function
提问by Gregory6106
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.
我想编写一个函数,该函数接受用户输入的日期,将其与搁置函数一起存储,并在调用时打印 30 天后的日期。
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.
我正在尝试编写一个简单的程序,它按照您指示我获取日期的方式获取日期并添加 30 天。
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'
这给了我一个错误: 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.
最终我想要的是 01/01/2013 + 30 天并打印 01/30/2013。
Thanks in advance!
提前致谢!
采纳答案by Martijn Pieters
The input()method can onlytake text from the terminal. You'll thus have to figure out a way to parse that text and turn it into a date.
该input()方法只能从终端获取文本。因此,您必须想办法解析该文本并将其转换为日期。
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)
要求用户分别输入一个日期的3个部分,所以调用
input()3次,把结果变成整数,构建一个日期:year = int(input('Enter a year')) month = int(input('Enter a month')) day = int(input('Enter a day')) date1 = datetime.date(year, month, 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. :-)
这两种方法都是例子;例如,没有包含错误处理,您需要阅读 Python 异常处理来自己弄清楚。:-)
回答by Arnold
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(xxx) 添加信息,这很好地解释了它。如下
datetime.datetime(year,month, day, hour, minute, second)with parameters all integer. It works!
datetime.datetime(year,month, day, hour, minute, second)参数全是整数。有用!
回答by thatPranav
Use the dateutils module
使用 dateutils 模块
from dateutil import parser
date = parser.parse(input("Enter date: "))

