Date to String conversion in Pyhton

2

I'm trying to get a date from this next select in python

select MAX(PAYMENT_DATE) from fact_cashflow
WHERE DOCUMENT_ID = 'SALDO FINAL' and PAYMENT_AMOUNT > 0

But my answer on the console is this

  

(datetime.date (2017, 2, 1))

I would like the date to be formatted in '2017-02-01'

How do I do it?

    
asked by anonymous 16.03.2017 / 19:19

2 answers

2

The date_format function allows you to format the date returned in the Mysql database query:

select date_format(MAX(PAYMENT_DATE), '%Y-%m-%d') from fact_cashflow
WHERE DOCUMENT_ID = 'SALDO FINAL' and PAYMENT_AMOUNT > 0;
    
16.03.2017 / 19:23
3

You can use .strftime('%Y-%m-%d')

In this way:

t = datetime.date(2017, 2, 1)
t.strftime('%Y-%m-%d')

Your response will be:     '2017-02-01'

    
16.03.2017 / 19:24