Date Ordinal Output

Date Ordinal Output?

Or shorten David's answer with:

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

python format datetime with st, nd, rd, th (english ordinal suffix) like PHP's S

The django.utils.dateformat has a function format that takes two arguments, the first one being the date (a datetime.date [[or datetime.datetime]] instance, where datetime is the module in Python's standard library), the second one being the format string, and returns the resulting formatted string. The uppercase-S format item (if part of the format string, of course) is the one that expands to the proper one of 'st', 'nd', 'rd' or 'th', depending on the day-of-month of the date in question.

How do you format the day of the month to say 11th, 21st or 23rd (ordinal indicator)?

// https://github.com/google/guava
import static com.google.common.base.Preconditions.*;

String getDayOfMonthSuffix(final int n) {
checkArgument(n >= 1 && n <= 31, "illegal day of month: " + n);
if (n >= 11 && n <= 13) {
return "th";
}
switch (n % 10) {
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
default: return "th";
}
}

The table from @kaliatech is nice, but since the same information is repeated, it opens the chance for a bug. Such a bug actually exists in the table for 7tn, 17tn, and 27tn (this bug might get fixed as time goes on because of the fluid nature of StackOverflow, so check the version history on the answer to see the error).

Display the date, like May 5th, using pythons strftime?

strftime doesn't allow you to format a date with a suffix.

Here's a way to get the correct suffix:

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

found here

Update:

Combining a more compact solution based on Jochen's comment with gsteff's answer:

from datetime import datetime as dt

def suffix(d):
return 'th' if 11<=d<=13 else {1:'st',2:'nd',3:'rd'}.get(d%10, 'th')

def custom_strftime(format, t):
return t.strftime(format).replace('{S}', str(t.day) + suffix(t.day))

print custom_strftime('%B {S}, %Y', dt.now())

Gives:

May 5th, 2011

How to ordinal form date using DATE_FORMAT()?

The issue is %d and it should be %D

https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

  select DATE_FORMAT(NOW(), '%D %M %y , %r') ;
+-------------------------------------+
| DATE_FORMAT(NOW(), '%D %M %y , %r') |
+-------------------------------------+
| 27th June 16 , 02:57:00 PM |
+-------------------------------------+
1 row in set (0.04 sec)

Display date with ordinal suffix in PHP

You can specify any character that you want:

$newDate = date("l M d\t\h Y", strtotime($date));

But if you want to get 1st, 2nd, 3rd, 4th ... you should use:

$newDate = date("l M jS Y", strtotime($date));

How can I get ordinal dates in Jekyll?

I solved this by writing the following Jekyll plugin, which I placed in the _plugins directory with an arbitrary name, e.g. date.rb:

require 'date'
require 'facets/integer/ordinal'

module Jekyll
module DateFilter
def pretty(date)
"#{date.strftime('%e').to_i.ordinalize} #{date.strftime('%B')} #{date.strftime('%Y')}"
end
end
end

Liquid::Template.register_filter(Jekyll::DateFilter)

Then — having installed the facets Ruby gem — I was able to use {{ post.date | pretty }} in my site, and all was well.



Related Topics



Leave a reply



Submit