pratik veer
pratik veer

Reputation: 39

How to convert 24-hour time to 12-hour and send it through Django RestFramework

This data display timing in 24 hours using angular

The above image shows 24 hours timing using angular.


This data display timing in 12 hours in admin panel

The above image shows timing in 12 hours in the admin panel.


This is the model I want to fetch records sent to Angular

class Appointment(models.Model):

    service_requested = models.TextField()
    customer = models.ForeignKey(Customer, on_delete = models.CASCADE)
    appointment_date_time = models.DateTimeField()

I tried this but I got a 24-hour time. Is there any way to convert time to 12-hours. and then send it

queryset = Appointment.objects.all()

Is it the correct way to convert time to 12-hour?

from datetime import datetime

queryset = Appointment.objects.all().annotate( 

           date_time = datetime.strptime('appointment_date_time','%Y-%m-%d %I:%M %p')

           ).values('service_requested','customer','date_time')

Upvotes: 0

Views: 324

Answers (1)

pratik veer
pratik veer

Reputation: 39

It is simple, see this:

<td>{{book.appointment_date_time| date:'medium'}}</td>  

In html file i just add template filter.

For more detail about the date format see this https://docs.angularjs.org/api/ng/filter/date

Upvotes: 1

Related Questions