skirando
skirando

Reputation: 31

Django how to manage format time with only hour and minute

In django form, I would like an input for a time with a format only with hour and minutes. But I keep having second on my web page. Anyone could help Thank you

models.py

Class Recettes(models.Model) :
    …
    temps_preparation = models.TimeField(
            db_column='Temps_Preparation',
            help_text = 'veuillez saisir le temps sous la forme heure:minute(HH:MM)',
            blank=True,
         null=True,
            ) 
….

settings.py

...
TIME_INPUT_FORMATS = [ '%I:%M']

forms.py

class RecetteForm(forms.ModelForm) :
    temps_préparation = TimeField(input_formats = ['%H:%M',], label='Temps de préparation – HH:MM')

Upvotes: 3

Views: 12814

Answers (4)

c24b
c24b

Reputation: 5552

Using TimeField and setting seconds to 00 before

See: TimeField Model in Doc

Upvotes: 0

Partha Prateem Patra
Partha Prateem Patra

Reputation: 181

You need to add a single line to the settings.py file and it will change the time formatting across all the apps in your project.

add this to the settings.py file:

  • If you want the 12 Hr clock formatting with AM/PM: TIME_INPUT_FORMATS = ('%I:%M %p',)
  • If you want 24 Hour clock formatting: TIME_INPUT_FORMATS = ('%H:%M',)

Upvotes: 5

Maede
Maede

Reputation: 182

in setting.py add

'DATETIME_FORMAT': "%m-%d - %M:%S"

in models.py

temps_preparation = models.DateTimeField(null=True)

and in form.py

datetime.strptime(yourdate, "%M:%S")

Upvotes: 1

Dos
Dos

Reputation: 2552

You can format dates directly in the html template (docs)

<p>{{ your_date|date:"h:iA" }}</p>

or in the view (docs)

your_date.strftime("%I:%M%p")

In the example above I used the 12-hours format.

Upvotes: 0

Related Questions