Date Ordinal Output?

后端 未结 14 1786
囚心锁ツ
囚心锁ツ 2020-11-27 06:38

I\'m wondering if there is a quick and easy way to output ordinals given a number in python.

For example, given the number 1, I\'d like to output

14条回答
  •  臣服心动
    2020-11-27 07:01

    I had to convert a script over from javascript where I had a useful fn that replicated phps date obj. Very similar

    def ord(n):
        return str(n)+("th" if 4<=n%100<=20 else {1:"st",2:"nd",3:"rd"}.get(n%10, "th"))
    

    this tied in with my date styler:

    def dtStylish(dt,f):
        return dt.strftime(f).replace("{th}", ord(dt.day))
    

    ps -I got here from another thread which was reported as a duplicate but it wasn't entirely since that thread also addressed the date issue

提交回复
热议问题