在 Python 中将日期从 mm/dd/yyyy 转换为另一种格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22739015/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:32:31  来源:igfitidea点击:

Convert date from mm/dd/yyyy to another format in Python

pythondatetimepython-3.x

提问by user3307366

I am trying to write a program that asks for the user to input the date in the format mm/dd/yyyy and convert it. So, if the user input 01/01/2009, the program should display January 01, 2009. This is my program so far. I managed to convert the month, but the other elements have a bracket around them so it displays January [01] [2009].

我正在尝试编写一个程序,要求用户以 mm/dd/yyyy 格式输入日期并进行转换。所以,如果用户输入 01/01/2009,程序应该显示 2009 年 1 月 1 日。这是我目前的程序。我设法转换了月份,但其他元素在它们周围有一个括号,所以它显示了一月 [01] [2009]。

date=input('Enter a date(mm/dd/yyy)')
replace=date.replace('/',' ')
convert=replace.split()
day=convert[1:2]
year=convert[2:4]
for ch in convert:
    if ch[:2]=='01':
        print('January ',day,year )

Thank you in advance!

先感谢您!

采纳答案by alecxe

Don't reinvent the wheel and use a combination of strptime()and strftime()from datetimemodule which is a part of python standard library (docs):

不要重新发明轮子并使用strptime()strftime()fromdatetime模块的组合,它是 python 标准库(docs)的一部分:

>>> from datetime import datetime
>>> date_input = input('Enter a date(mm/dd/yyyy): ')
Enter a date(mm/dd/yyyy): 11/01/2013
>>> date_object = datetime.strptime(date_input, '%m/%d/%Y')
>>> print(date_object.strftime('%B %d, %Y'))
November 01, 2013

回答by Snoop Dogg

Split it by the slashes

用斜线分割

convert = replace.split('/')

and then create a dictionary of the months:

然后创建一个月份的字典:

months = {1:"January",etc...}

and then to display it do:

然后显示它:

print months[convert[0]] + day + year

回答by shrnkrn

You might want to look into python's datetime library which will take care of interpreting dates for you. https://docs.python.org/2/library/datetime.html#module-datetime

您可能想查看 python 的 datetime 库,它将为您解释日期。https://docs.python.org/2/library/datetime.html#module-datetime

from datetime import datetime
d = input('Enter a date(mm/dd/yyy)')

# now convert the string into datetime object given the pattern
d = datetime.strptime(d, "%m/%d/%Y")

# print the datetime in any format you wish.
print d.strftime("%B %d, %Y") 

You can check what %m, %d and other identifiers stand for here: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

您可以在此处查看 %m、%d 和其他标识符的含义:https: //docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

回答by behzad.nouri

As a suggestion use dateutil, which infers the format by itself:

作为建议使用dateutil,它自己推断格式:

>>> from dateutil.parser import parse
>>> parse('01/05/2009').strftime('%B %d, %Y')
'January 05, 2009'
>>> parse('2009-JAN-5').strftime('%B %d, %Y')
'January 05, 2009'
>>> parse('2009.01.05').strftime('%B %d, %Y')
'January 05, 2009'