Skip to content Skip to sidebar Skip to footer

Getting Proper Date Format From Sql Search Results (datetime.datetime)

This is my first time using cx_oracle. Part of my project is to create a search engine that processes 'SELECT FROM..' queries and displays them. This is for searching patient infor

Solution 1:

datetime is the date object of python, with that you could get any format you want in your case you can get your format with:

import datetime
row[3].strftime('%y-%m-%d')

Now for clarification of what is happening. With strftime() you can get a Date or Time String out of your Datetimeobject. with '%y-%m-%d' I formated it.

row[3] should be your dateobject element.

%y returns the Year with 2 digits like 14 for 2014. If you want the whole year you have to type %Y

%m returns the number of month and %d the number of day.

there are also options like weekday, the name of the month, etc.

you can read about that in the docs

So now your code should look like this:

import datetime

#your code to read the file

forrowinrows:
    row[3] =row[3].strftime('%y-%m-%d')
    print(row)

#some other code

Post a Comment for "Getting Proper Date Format From Sql Search Results (datetime.datetime)"