Home > Blockchain >  Can django queryset date be formatted from 'DD:MM:YYYY HH:MM:SS' TO 'YYYY:MM:DD'
Can django queryset date be formatted from 'DD:MM:YYYY HH:MM:SS' TO 'YYYY:MM:DD'

Time:03-20

My desired output is : { " formated_date:'2021-05-01' ", }

Queryset returns : {date : '2021-05-01 12:01:20'}

CodePudding user response:

I got this working for MySql database:

queryset.annotate(release_date=
   Func(F('temp_date'),
   Value('%Y-%m-%d'),
   function='DATE_FORMAT',
   output_field=CharField())

CodePudding user response:

Based on this answer you could make s.th. like

from django.db.models import F, Func, Value, DateField

qs.annotate(
  formatted_date=Func(
    F('your_field'),
    Value('DD-MM-YYYY HH:MM:SS'),
    function='to_char',
    output_field=DateField()
  )
)
  • Related