开发者

Format date like: Monday, 1st March

开发者 https://www.devze.com 2023-01-10 23:16 出处:网络
Seems like it should be simple enough but it\'s driving me up the wall. I\'ve looked at the python date formatting strings and it still doesn\'t make too much sense.

Seems like it should be simple enough but it's driving me up the wall. I've looked at the python date formatting strings and it still doesn't make too much sense.

Here's what I'm trying to do: <Full day>, <Day of month>开发者_开发技巧<Ordinal> <Month>

Where <Ordinal> is st, nd, rd, th, etc depending on the day.


Update 2: Looks like OP found this useful after all :)

Update: Never mind. The OP was looking for Django date formatting, not Python.

AFAIK there is no built in format specifier for the ordinal. The others are easy:

my_date.strftime('%A, %d %B')

I found this solution on the web:

if 4 <= day <= 20 or 24 <= day <= 30:
    suffix = "th"
else:
    suffix = ["st", "nd", "rd"][day % 10 - 1]


Just found the answer in the docs. God knows why they're not linked to from every reference to date formatting.

{{e.when|date:"l, jS F"}}

Outside of templates:

from django.utils import dateformat
dateformat.format(datetime.now(), 'l, jS F')
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号