开发者

reading and writing date time information in database/Oracle

开发者 https://www.devze.com 2023-02-25 06:22 出处:网络
I queried a S开发者_运维百科QL table to arrive at the following list L L= [\'S14\', \'G7\', \'1\', datetime.datetime(2011, 1, 30, 0, 0), \'R13\', 100000]

I queried a S开发者_运维百科QL table to arrive at the following list L

L= ['S14', 'G7', '1', datetime.datetime(2011, 1, 30, 0, 0), 'R13', 100000]

I want to first change the date format to (yy,mm,dd) which I managed to do by

now=L[3]

new_format=now.date().isoformat()

which gave me (2011-01-30)

I need this format of the date to do some calculations/date comparisons

finally I want to write this information back to the database

with the format changed to (mm/dd/yy). How do i do this?

please suggest


You can use:

datetime.datetime.strptime(mdyDate, 'm/d/y')

to get the string back to datetime.

But why don't you use datetime.datetime the whole time? It is perfect for calculations/date comparisons.


If you want to have Python datetime as 'MM/DD/YYYY' then use strftime("%m/%d/%y"), but Python have great functions/methods that work on datetime objects so comparing dates as stings is not necessary.

If you want to insert such data to Oracle database you can use it as literal and use TO_DATE() to show what format you are using:

TO_DATE('01/23/2011', 'MM/DD/YYYY')

You can even set datetime format for Oracle session, and then use date as literals without TO_DATE():

alter session set nls_date_format='MM/DD/YYYY'

(I use it with format: YYYY-MM-DD HH24:MI:SS so you must test it yourself)

0

精彩评论

暂无评论...
验证码 换一张
取 消