Thursday, February 15, 2018

Django Admin DateTimeField Showing 24hr format time

Leave a Comment

I tried on google but i did not found the solution , In Django Admin side , i showing start date and end date with time . But time is in 24 hr format i want to it in 12 hr format

class CompanyEvent(models.Model):     title = models.CharField(max_length=255)     date_start = models.DateTimeField('Start Date')     date_end = models.DateTimeField('End Date')     notes = models.CharField(max_length=255)      class Meta:         verbose_name = u'Company Event'         verbose_name_plural = u'Company Events'      def __unicode__(self):         return "%s (%s : %s)" % (self.title, self.date_start.strftime('%m/%d/%Y'), self.date_end) 

I also found some thing but this is not helping me What i found

I am new in python and django please help me out .Screen Shot

3 Answers

Answers 1

This is a matter for django's settings, not the model: settings doc.

Check your TIME_INPUT_FORMATS in MyProject/MySite/settings.py and add this as necessary:

TIME_INPUT_FORMATS = [     '%I:%M:%S %p',  # 6:22:44 PM     '%I:%M %p',  # 6:22 PM     '%I %p',  # 6 PM     '%H:%M:%S',     # '14:30:59'     '%H:%M:%S.%f',  # '14:30:59.000200'     '%H:%M',        # '14:30' ] 

If the display of the time format on the changelist page is still wrong, check your LANGUAGE_CODE and USE_L10N settings.

Answers 2

Take a look at Django docs you will get to know format like this

'%Y-%m-%d %H:%M:%S' 

where %H is Hour, 24-hour format with leading zeros, to get 12-hour format replace it with %h

So you have to use- '%Y-%m-%d %h:%M:%S'

Answers 3

Defaultly django displays 24 hrs format, if you want to customize you need to specify the 12 hrs format. Let me know if this works

class CompanyEvent(models.Model): title = models.CharField(max_length=255) date_start = models.DateTimeField('Start Date') date_end = models.DateTimeField('End Date') notes = models.CharField(max_length=255)  class Meta:     verbose_name = u'Company Event'     verbose_name_plural = u'Company Events'  def __unicode__(self):     return "%s (%s : %s)" % (self.title, self.date_start.strftime('%m/%d/%Y %I:%M %p'), self.date_end) 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment