I am sorry to ask this silly question but I am stuck in converting date format (Jan 02, 2020) to another date format ('%Y-%m-%d'). Would anyone be able to give me a hand here? Thank you very much.
Asked
Active
Viewed 2,389 times
-1
-
4what have you tried ? – Abhishek Malik Aug 24 '20 at 01:56
-
you can find solution from here https://stackoverflow.com/questions/42980662/convert-string-with-month-name-to-datetime/42980752 – Anjaly Vijayan Aug 24 '20 at 02:08
-
Does this answer your question? [Convert String with month name to datetime](https://stackoverflow.com/questions/42980662/convert-string-with-month-name-to-datetime) – Ilya Berdichevsky Aug 24 '20 at 02:19
2 Answers
1
Generally, this is a two-step process:
- Convert the input into an internal representation, which in this case will almost certainly be either
date
ordatetime
. For simple cases where you know the format, the conversion can be done with thedatetime.strptime()
method. - Convert the internal representation into the required output form. In this case, that will be either
date.strftime()
ordatetime.strftime()
, depending on which internal representation you use; they both pretty much work the same way.

joshmeranda
- 3,001
- 2
- 10
- 24

Jiří Baum
- 6,697
- 2
- 17
- 17
-
I should note that this structure is applicable to a lot of problems in programming; many programs/functions have basically the same three-step structure: (1) deal with the input; (2) apply the needed transformation or calculation (in this case none); (3) prepare the output. – Jiří Baum Aug 24 '20 at 02:15
-
1
import datetime
temp = datetime.datetime.strptime('Jan 02, 2020', '%b %d, %Y')
result = temp.strftime('%Y-%m-%d')

Jiří Baum
- 6,697
- 2
- 17
- 17

Frank Yellin
- 9,127
- 1
- 12
- 22
-
data['Date'] = pd.to_datetime(data['Date'], format = '%b %d, %Y') Hi, but after I ran the above code, an error occurred. (valueError: time data 'Aug 19,2020' does not match format '%b %d, %Y' (match)). Would you mind to tell me where I went wrong? – Jacky Law Aug 24 '20 at 03:09
-
Formats are extremely precise. I think you forgot.a space after the comma. – Frank Yellin Aug 24 '20 at 03:20
-
-
-