简体   繁体   中英

How can I change this format to be datetime format? (python)

I have a dataset which contains some columns with datetime. My problem is, I found this type datetime format:

Apr'11
Apr-11
Apr 11

How can I automatically change this format to be datetime format?

for you can use datetime module

from datetime import datetime 

this is the link if you want any confusion

date_string = "Apr'11"
date = datetime.strptime(date_string, "%b'%d")

%b = Locale's abbreviated month name. (like Apr, Mar, Jan, etc) %d = Day of the month as a decimal number [01,31]

date_string_2 = "Apr-11"
date = datetime.strptime(date_string, "%b-%d")

date_string_3 = "Apr 11"
date = datetime.strptime(date_string, "%b %d")

You should write this "%b %d" same as like date_string otherwise it will give you, even if you give an extra space.

go to this link to learn more about this: https://docs.python.org/3/library/time.html

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