0

I currently have a table which is displaying on a HTML page, data which has been inputted into from a form. I think the format for the data in MySQL table saves as yyyy-mm-dd. I would like the date that is displayed on the table to be in the format DD-MM-YYYY.

I have tried looking at previous questions but cannot find a solution for this.

Below is a snippet of my code for the HTML table which pulls information from some of my model tables.

<tr>
           <td>
             {{ current_user.first_name }} {{ current_user.last_name }}
       </td>
          <td>
                {% if current_user.tasterday %}
                {{ current_user.tasterday.date }}
                 {% else %}
                      -
                 {% endif %}
          </td>
          <td>
                {% if current_user.course %}
                {{ current_user.course.name }}
                 {% else %}
                      -
                 {% endif %}
          </td>
            <td>
                {% if current_user.tasterday %}
                {{ current_user.tasterday.description }}
                 {% else %}
                      -
                 {% endif %}
          </td>

</tr>

Could someone please tell me what I need to add to the date section so that the format is DD-MM-YYY. Thankyou.

RianneJ
  • 143
  • 1
  • 11
  • _I think the format for the data in MySQL table saves as yyyy-mm-dd_ - Find out exactly how your dates are currently formatted then you can figure out how to convert them. If it's a Date object then `strftime()` will work. – noslenkwah Apr 30 '20 at 20:20
  • @noslenkwah I've just checked the format in MySQL and yes, it's saved as YYYY-MM-DD. Would I need to convert how it is formatted in the table to be able to change the way it is displayed? – RianneJ Apr 30 '20 at 20:24
  • No need to change your database. Converting from YYYY-MM-DD to DD-MM-YYYY is easy. The most robust way I can think of is first [converting it to a Date object](https://stackoverflow.com/questions/2803852/python-date-string-to-date-object) and then formatting with [strftime](https://jingwen-z.github.io/converting-between-datetime-and-string/). strftime can be done inside your jinja2 template or with Flask. – noslenkwah Apr 30 '20 at 20:29
  • @noslenkwah I've followed the answer and guide you sent, entering it into my python console, but when i run my program, the date still shows in the YYYY-MM-DD format, what am i doing wrong? – RianneJ Apr 30 '20 at 21:20
  • What is `current_user.tasterday.date`? String? Date? DateTime? – noslenkwah Apr 30 '20 at 21:21
  • @noslenkwah It's a date. db.Date (and date is a column in the tasterday table) – RianneJ Apr 30 '20 at 21:27
  • 1
    Then you can skip the 'Convert to a Date object'. Posted an answer below. – noslenkwah Apr 30 '20 at 21:41

2 Answers2

1

You can format your Date Object using strftime in jinja2.

{{ current_user.tasterday.date.strftime('%d-%m-%Y') }}

If you want to change the format this is a good resource.

noslenkwah
  • 1,702
  • 1
  • 17
  • 26
0

I think this python script should help. It displays current time in dd/mm/yyyy,you can adapt it for your project. Also consider changing the way the date is stored in the db from your database structure.

import datetime

x = datetime.datetime.now()

print(x.strftime("%x"))