Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make pythons datetime object show AM and PM in lowercase?

Over here: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior it says that %p displays AM / PM. It shows this

AM, PM (en_US);
am, pm (de_DE)

I'm not sure what de_DE is, but when I do %p using Django Rest Framework it shows AM and PM in capital letters. How do I change it so that am and pm are displayed in lowercase?

This is my DRF settings.py code where I edit the datetime format (might not be useful):

#added REST_FRAMEWORK
REST_FRAMEWORK = {
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    ),
    'DATETIME_FORMAT': "%b %d at %I:%M %p"
}
like image 355
SilentDev Avatar asked Dec 04 '22 00:12

SilentDev


2 Answers

Try changing '%p' to '%P'. Whether '%P' is supported depends on the 'strftime' function provided by the platform C library.

like image 138
acw1668 Avatar answered Mar 16 '23 01:03

acw1668


I'm pretty sure that you cannot. The %p format code is the locale-specific "national representation of either "ante meridiem" (a.m.) or "post meridiem" (p.m.) as appropriate". de_DE is the generic German locale which is what defines the national representation which happens to be lowercase. The en_US locale has it defined in uppercase. Part of using functions like strftime is that they take care of the localization details for you.

The documentation does explicitly mention that the exact values are entirely locale specific:

Because the format depends on the current locale, care should be taken when making assumptions about the output value. Field orderings will vary (for example, “month/day/year” versus “day/month/year”), and the output may contain Unicode characters encoded using the locale’s default encoding (for example, if the current locale is ja_JP, the default encoding could be any one of eucJP, SJIS, or utf-8; use locale.getlocale() to determine the current locale’s encoding).

like image 30
D.Shawley Avatar answered Mar 15 '23 23:03

D.Shawley