2011-05-14 20 views
6

Güne, günün saatine göre "st", "nd", "rd" veya "th" veya sonra "th" ifadesini gizlemeye çalışıyorum. javascript böylece nereden başlayacağınızı hiçbir fikrim yok.Dönüştürme tarihi günü (05/12/2011 - 12th)

Ör

05/01/2011 = 1
05.02.2011 = 2
05.03.2011 = 3
05/12/2011 = 12.
05/22/2011 = 22nd

Teşekkürler

+0

İlgili (... 36021 vb 1013th) çok daha yüksek değerlere hitap edecek -a-date-in-javascript – Orbling

+0

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

cevap

10

Birincisi, tarihi almak:

function get_nth_suffix(date) { 
    switch (date) { 
    case 1: 
    case 21: 
    case 31: 
     return 'st'; 
    case 2: 
    case 22: 
     return 'nd'; 
    case 3: 
    case 23: 
     return 'rd'; 
    default: 
     return 'th'; 
    } 
} 

demo http://jsfiddle.net/DZPSw/

at:

var date = myval.getDate(); 

Sonra eki bulmak

+0

Teşekkürler.Bir anahtar olarak yeterince basit görünüyor :) – JohnBoy

+0

oldukça verimli olmalı, ancak her durumda performans yerine okunabilirlik için gidiyordum. – Alnitak

2

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 :-)

+0

Giriş için teşekkürler. Ayrıca 12, 13 = th bu belirtilen girişimler;) – JohnBoy

+0

@JohnBoy: Evet, farkettim ve düzelttim :) – mellamokb

9
var date = new Date('05/12/2011').getDate(), 
ordinal = date + (date>10 && date<20 ? 'th' : {1:'st', 2:'nd', 3:'rd'}[date % 10] || 'th'); 

veya

ordinal = date + ([,'st','nd','rd'][/1?.$/.exec(date)] || 'th'); 
+0

Bu en doğru yöntem. Ancak, başlangıç ​​koşulunu "date> 3 && <21" olarak değiştirdiyseniz daha iyi bir performans elde edersiniz. – Orbling

+0

belki, ama bence bu daha mantıklı bir mantığa sahip olduğu anlamına geliyor, çünkü "teens'leri hariç tutuyorum" ( –

+0

) Muhtemelen, evet. – Orbling

1

Geçen gün bu basit işlevi yazdım. http://stackoverflow.com/questions/1056728/formatting: bir tarih için daha büyük sayılar gerekmese de, bu

var fGetSuffix = function(nPos){ 

    var sSuffix = ""; 

    switch (nPos % 10){ 
     case 1: 
      sSuffix = (nPos % 100 === 11) ? "th" : "st"; 
      break; 
     case 2: 
      sSuffix = (nPos % 100 === 12) ? "th" : "nd"; 
      break; 
     case 3: 
      sSuffix = (nPos % 100 === 13) ? "th" : "rd"; 
      break; 
     default: 
      sSuffix = "th"; 
      break; 
    } 

    return sSuffix; 
};