Sen Günü numarası almak için JavaScript Date/Time Functions ile başlayabilir:
var theDate = myDateObj.GetDate(); // returns 1-31
Sonra uygun soneki almak için bir kural yazmak gerekir. Çoğu zaman istisnalar hariç th
olacaktır. Istisnalar nelerdir? 1, 21, 31 = st
, 2, 22 = nd
, 3, 23 = rd
, diğer her şey th
'dur. http://jsfiddle.net/6Nhn8/
Yoksa sıkıcı ve use a library olabilir: 1-31 için
İşte
var nth = '';
if (theDate > 3 && theDate < 21) // catch teens, which are all 'th'
nth = theDate + 'th';
else if (theDate % 10 == 1) // exceptions ending in '1'
nth = theDate + 'st';
else if (theDate % 10 == 2) // exceptions ending in '2'
nth = theDate + 'nd';
else if (theDate % 10 == 3) // exceptions ending in '3'
nth = theDate + 'rd';
else
nth = theDate + 'th'; // everything else
sonlar gösteren bir çalışma demo: Bu yüzden, bu oran 1 ile biten olmadığını kontrol etmek 2 veya 3 mod %
kullanabilirsiniz :-)
İlgili (... 36021 vb 1013th) çok daha yüksek değerlere hitap edecek -a-date-in-javascript – Orbling
Tarihte '.getDate()' i çağırmak ve karar vermek için bazı mantık kullanmanız gerekebilir, 1/21/31 = * st *, 2/22 = * nd * , 3/23 = * rd *, else * th *. – Orbling