简体   繁体   中英

Convert date into a specific format in python irrespective of the input format is

I have a problem statement to convert the dates into a specific format. However, the input can be of any format. For example,

Input Desired_output
2020/11/20 2020-11-20
20201120 2020-11-20
20202011 2020-11-20
11/20/2020 2020-11-20
202020Nov 2020-11-20

I'm able to solve where is a delimiter present in between year, month and date using the Dateutil package. But it is not able to solve where is no clear delimiter.

Is there any way to solve this problem?

I don't think there is an easy way to universally parse dates that don't follow the standard formats . You'll have to specify the format in which the date is written for it to be parsed, which you can easily do using strptime (see here for a reference on datetime formatting).

A package like dateutil can be helpful as well. You should take a look, however, at the accepted date formats . In general, dateutil is more convenient, but strptime gives you more control and ability to parse more date formats.

Four out of the five examples you mentioned can be parsed using dateutil as follows:

from dateutil import parser
parser.parse("2020/11/20")   # Output: datetime.datetime(2020, 11, 20, 0, 0)
parser.parse("11/20/2020")   # Output: datetime.datetime(2020, 11, 20, 0, 0)

For the two other examples, the dayfirst and yearfirst arguments are needed to let dateutil parse them correctly:

parser.parse("20201120", yearfirst=True)
parser.parse("20202011", yearfirst=True, dayfirst=True)
# Output for both: datetime.datetime(2020, 11, 20, 0, 0)

Finally, I'm not aware of a way to parse the date "202020Nov" using dateutil ; however, it can be parsed using strptime as follows:

from datetime import datetime
datetime.strptime("202020Nov", "%Y%d%b")
# Output: datetime.datetime(2020, 11, 20, 0, 0)

All the best.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM