5

After hours of searching, I found many posts that are related but wasn't able to help.

What I want to do is input eg: 10:30 AM into the TimeField.
In the django rest framework API on the browser, it is using this 10:30 AM format ('%I:%M %p').
But when I am using postman to test it, the output is in 24hr format ('%H:%M:%S'). I also tried to use 10:30 PM as input but the output I get is 10:30:00 instead of 22:30:00.

Many of the answers I found suggest to change the TimeField format in settings.py by using this line:

TIME_INPUT_FORMATS = ('%I:%M %p',)

but it doesn't work for me.

Sorry for my inexperience on django rest framework as I am still learning.

Here is the screenshot of the result. On browser API:

On browser API

On postman:

On postman

John Moutafis
  • 22,254
  • 11
  • 68
  • 112
Jin Nii Sama
  • 707
  • 1
  • 16
  • 33

2 Answers2

8

If you check the documentation on the TimeField you will see:

Signature: TimeField(format=api_settings.TIME_FORMAT, input_formats=None)

Where

format - A string representing the output format. If not specified, this defaults to the same value as the TIME_FORMAT settings key, which will be 'iso-8601' unless set. Setting to a format string indicates that to_representation return values should be coerced to string output. Format strings are described below. Setting this value to None indicates that Python.

input_formats - A list of strings representing the input formats which may be used to parse the date. If not specified, the TIME_INPUT_FORMATS setting will be used, which defaults to ['iso-8601'].

So you either can specify the format and input_formats on the serializer, or set the settings.TIME_FORMAT and settings.TIME_INPUT_FORMATS.

Let's set the first case:

class MySerializer(serializers.Serializer):
    ...
    birthTime=serializers.TimeField(format='%I:%M %p', input_formats='%I:%M %p')

Some suggestions:

  1. Make your variable names snake case: birth_time.
  2. You may need to play a bit around with the input format because you may expect many different inputs:

    input_formats=['%I:%M %p','%H:%M',...]
    
John Moutafis
  • 22,254
  • 11
  • 68
  • 112
  • I tried using this in serializer.py `class MyUserSerializer(serializers.ModelSerializer): birthTime = serializers.TimeField(format='%I:%M %p', input_format='%I:%M %p') class Meta: model = MyUser fields = ('userId', 'username', 'email', 'password', 'first_name', 'last_name', 'gender', 'nric', 'birthday', 'birthTime', 'ethnicGroup', 'mobileNo', 'favoriteClinic',)` but i got this error instead: ` __init__() got an unexpected keyword argument 'input_format'` Sorry for the line spacing, shift+enter doesnt work . – Jin Nii Sama Nov 24 '17 at 09:16
  • @JinNiiSama That was my fault, the correct argument is `input_formats` (I edited the answer as well) – John Moutafis Nov 24 '17 at 09:35
  • Alright, the error is gone but i have another problem, https://imgur.com/a/AA8d2 Im pretty sure this is the correct format but i end up getting error for the format. Now im getting confuse with the format. Sorry for these small trouble – Jin Nii Sama Nov 27 '17 at 05:42
  • 2
    I solve the problem with this: `valid_time_formats = ['%H:%M', '%I:%M%p', '%I:%M %p']` Then in the input_formats=valid_time_formats. Now it work properly – Jin Nii Sama Nov 28 '17 at 03:37
  • @JinNiiSama good to know, If my answer helped you, you can accept it :) – John Moutafis Nov 28 '17 at 10:33
  • this works for me: **time_day_start = serializers.TimeField(format="%I:%M %p", input_formats=["%I:%M %p", "iso-8601"])**, otherwise you wil have validation errors on write – Despota Feb 14 '23 at 21:39
-1

Convert the result in Serializer validate method and return it.

import time
t = time.strptime(timevalue_24hour, "%H:%M")
timevalue_12hour = time.strftime( "%I:%M %p", t )