'How can we change this date string 13MAY2022 into datetime format in python/ [duplicate]
This is my date string myString = "13MAY2022"
Can anyone tell me please how can we change this myString into datetime format in python?
Solution 1:[1]
This should work:
>>> import datetime
>>> myString = "13MAY2022"
>>> dateformat = datetime.datetime.strptime(myString, "%d%b%Y")
>>> dateformat
datetime.datetime(2022, 5, 13, 0, 0)
strptime is a method to convert date strings into date format. It's first argument is our string and the second argument is locale references in the same order and patter our string has.
For example your string has no spaces in between and is in DateMonthYear order. Suppose you string looks like : 13-May-2022, then the second argument would look like: "%d-%b-%Y"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Shivam Pandya |