Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day-of-month with ordinal suffix. #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ function newYear(y) {
return {y: y, m: 0, d: 1, H: 0, M: 0, S: 0, L: 0};
}

function empty() {
return "";
}

export default function(locale) {
var locale_dateTime = locale.dateTime,
locale_date = locale.date,
Expand All @@ -30,7 +34,8 @@ export default function(locale) {
locale_weekdays = locale.days,
locale_shortWeekdays = locale.shortDays,
locale_months = locale.months,
locale_shortMonths = locale.shortMonths;
locale_shortMonths = locale.shortMonths,
locale_ordinal = locale.ordinal || empty;

var periodRe = formatRe(locale_periods),
periodLookup = formatLookup(locale_periods),
Expand All @@ -56,6 +61,7 @@ export default function(locale) {
"j": formatDayOfYear,
"L": formatMilliseconds,
"m": formatMonthNumber,
"o": formatDayOfMonthOrdinal,
"M": formatMinutes,
"p": formatPeriod,
"S": formatSeconds,
Expand Down Expand Up @@ -83,6 +89,7 @@ export default function(locale) {
"j": formatUTCDayOfYear,
"L": formatUTCMilliseconds,
"m": formatUTCMonthNumber,
"o": formatUTCDayOfMonthOrdinal,
"M": formatUTCMinutes,
"p": formatUTCPeriod,
"S": formatUTCSeconds,
Expand Down Expand Up @@ -110,6 +117,7 @@ export default function(locale) {
"j": parseDayOfYear,
"L": parseMilliseconds,
"m": parseMonthNumber,
"o": null, // TODO
"M": parseMinutes,
"p": parsePeriod,
"S": parseSeconds,
Expand Down Expand Up @@ -146,7 +154,7 @@ export default function(locale) {
if (specifier.charCodeAt(i) === 37) {
string.push(specifier.slice(j, i));
if ((pad = pads[c = specifier.charAt(++i)]) != null) c = specifier.charAt(++i);
else pad = c === "e" ? " " : "0";
else pad = c === "e" ? " " : c === "o" ? "" : "0";
if (format = formats[c]) c = format(date, pad);
string.push(c);
j = i + 1;
Expand Down Expand Up @@ -247,6 +255,11 @@ export default function(locale) {
return parseSpecifier(d, locale_time, string, i);
}

function formatDayOfMonthOrdinal(d, p) {
var v = d.getDate();
return pad(v, p, 2) + locale_ordinal(v);
}

function formatShortWeekday(d) {
return locale_shortWeekdays[d.getDay()];
}
Expand All @@ -267,6 +280,11 @@ export default function(locale) {
return locale_periods[+(d.getHours() >= 12)];
}

function formatUTCDayOfMonthOrdinal(d, p) {
var v = d.getUTCDate();
return pad(v, p, 2) + locale_ordinal(v);
}

function formatUTCShortWeekday(d) {
return locale_shortWeekdays[d.getUTCDay()];
}
Expand Down
5 changes: 4 additions & 1 deletion src/locale/en-US.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {ordinal} from "./en";

export default {
dateTime: "%a %b %e %X %Y",
date: "%m/%d/%Y",
Expand All @@ -6,5 +8,6 @@ export default {
days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
shortDays: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
shortMonths: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
shortMonths: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
ordinal: ordinal
};
5 changes: 5 additions & 0 deletions src/locale/en.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var ordinals = ["th", "st", "nd", "rd"];

export function ordinal(x) {
return ordinals[(3 < x && x < 21) || (x %= 10) > 3 ? 0 : x];
};