Convert date from dd-mm-yy to dd-mm-yyyy using python [duplicate]

2024/10/8 6:29:39

I have a date input 'date_dob' which is '20-Apr-53' I tried converting this to format yyyy-mm-dd using following code:

print pd.to_datetime(date_dob,format = '%d-%b-%y')

which returns '2053-04-20 00:00:00' instead of '1953-04-20 00:00:00'

Answer

You need to explicitly check for the year, and then subtract it by 100 years as:

>>> import datetime>>> my_date = '20-Apr-53'
>>> d = datetime.datetime.strptime(my_date, '%d-%b-%y')
>>> d
datetime.datetime(2053, 4, 20, 0, 0)
#                   ^ year = 2053  
>>> if d.year > 2000:
...     d = d.replace(year=d.year-100)  # subtract year by 100
... 
>>> d
datetime.datetime(1953, 4, 20, 0, 0)
#                   ^ year = 1953
>>> d.strftime('%Y-%m-%d') # %Y instead of %y, reason mentioned below
'1953-04-20'
#  ^ all four digit of year

Using %Y instead of %y because:

  • %y: displays last two digit of year

  • %Y: display all four digit of year

https://en.xdnf.cn/q/118725.html

Related Q&A

sklearn pipeline transform ValueError that Expected Value is not equal to Trained Value

Can you please help me to with the following function where I got the error of ValueError: Column ordering must be equal for fit and for transform when using the remainder keyword(The function is calle…

How to show Chinese characters in Matplotlib graphs?

I want to make a graph based on a data frame that has a column with Chinese characters. But the characters wont show on the graph, and I received this error. C:\Users\march\anaconda3\lib\site-packages\…

nginx flask aws 502 Bad Gateway

My server is running great yesterday but now it returned a 502 error, how could this happen?In my access.log shows:[24/Aug/2016:07:40:29 +0000] "GET /ad/image/414 HTTP/1.1" 502 583 "-&q…

Let discord bot interact with other bots

I have a Python script for a Discord bot and I want it to send a message to another Bot and select the prompt option and then type in a message but I cant get the interaction done. It just sends the me…

Image has 3 channels but its in a grayscale color. If I change it to 1 channel, it goes into RGB

I started doing some image-processing in python and Ive stumbled upon an issue which is kind of confusing from a beginners perspective. I have a dataset of 1131 np arrays (images) of MRI on knee. The s…

Creating a Barplot using pyqt

I need plotting an animated bar chart with pyqtgraph. With animate i mean a chart, which updates his values given by a serial port. For now, a not-animated plot will be enough. I would like to implemen…

Stop Button in Tkinter

Im trying to have a turtle animation start with a button and stop with a button. Its very easy to start with a button but I cant seem to be able to figure out a stop button? Heres my code so far: imp…

binascii.Error: Incorrect padding How to decode the end with /

I received a string encoded with base64, I am using python to decode it, but decoding failed, I found that the string is followed by / ends, I dont know how to decode it, I havent found the answer, who…

How to match words in a list with user input in python?

I am working on program which takes user input and replaces the words in a list with x. eg is the word is sucks and user input is "this word is sucks". the output should be "this word i…

How to plot a ROC curve using dataframe converted from CSV file

I was trying to plot a ROC curve by using the documentation provided by sklearn. My data is in a CSV file, and it looks like this.It has two classes Goodand Badscreenshot of my CSV fileAnd my code look…