2010-12-28 2 views
14

Come posso ottenere sotto il formato di data cita in C#.Come faccio a produrre un formato di data come "1 novembre" in C#

  • Per 1-nov-2010 dovrebbe essere display come: 1 novembre

  • Per 30-Nov-2010 dovrebbe essere display: 30

    novembre

Possiamo fare utilizzando qualsiasi formato data o crea una funzione personalizzata che ritorna per 1 -> 'st', 2-> 'nd' 3-> 'rd', qualsiasi data no -> 'th'.

risposta

32

Il seguente codice è basato su quello answer che genera un numero ordinale da un numero intero:

public static string ToOrdinal(int number) 
{ 
    switch(number % 100) 
    { 
     case 11: 
     case 12: 
     case 13: 
      return number.ToString() + "th"; 
    } 

    switch(number % 10) 
    { 
     case 1: 
      return number.ToString() + "st"; 
     case 2: 
      return number.ToString() + "nd"; 
     case 3: 
      return number.ToString() + "rd"; 
     default: 
      return number.ToString() + "th"; 
    } 
} 

di quanto si può generare la stringa di output:

+0

risposta davvero stupenda :) –

+0

È davvero fantastico. Risparmia molto tempo. :) – SarangK

0

Sono quasi sicuro che non esiste una routine datatime per mostrare la data come 1o o 30o.

Recentemente ho scritto del codice come quello da zero. Penso che dovrai fare lo stesso.

Non ho il mio codice a portata di mano, ma ho appena creato una serie di stringhe con le lettere per ogni numero ("th", "st", "nd", "rd", "th", ecc.). Quindi mod contro 10 e usa il resto come indice nell'array. Puoi semplicemente aggiungere quella stringa al tuo numero.

+0

Anche io penso lo stesso. –

+1

mod contro 10 non è abbastanza buono, perché allora avresti l'11 e il 12. –

+0

Sì, so di averlo fatto funzionare. Credo di avere alcune eccezioni per i ragazzi. –

0

È possibile utilizzare Regular Expressions per estrarre il giorno e il mese. Quindi, memorizzare tutti i nomi dei mesi in un array e utilizzare .startsWith per ottenere il nome corretto del mese. Puoi usare un semplice case per vedere se hai bisogno di "st", "nd", "rd" o "th".

1

somthing come questo dovrebbe funzionare ...

using System; 
using System.Text; 

namespace Program { 


class Demo { 
     static string[] extensions = 
     // 0  1  2  3  4  5  6  7  8  9 
     { "th", "st", "nd", "rd", "th", "th", "th", "tn", "th", "th", 
     // 10 11 12 13 14 15 16 17 18 19 
      "th", "th", "th", "th", "th", "th", "th", "tn", "th", "th", 
     // 20 21 22 23 24 25 26 27 28 29 
      "th", "st", "nd", "rd", "th", "th", "th", "tn", "th", "th", 
     // 30 31 
      "th", "st" }; 

    public static void Main() { 
    String strTestDate = "02-11-2007"; 
    DateTime coverdate = DateTime.ParseExact(strTestDate, "dd-MM-yyyy", null); 
    string s = coverdate.ToString(" MMMM yyyy"); 
    string t = string.Format("{0}{1}",coverdate.Day,extensions[coverdate.Day]); 
    string result = t + s; 


    } 
    } 
} 
2

Quindi ecco una soluzione completa con i metodi di estensione. Funziona con C# 3.0 e versioni successive. lavoro lo più plagiato di Nikhil:

public static class DateTimeExtensions 
{ 
     static string[] extensions = // 0 1 2 3 4 5 6 7 8 9 
      { "th", "st", "nd", "rd", "th", "th", "th", "tn", "th", "th", 
       // 10 11 12 13 14 15 16 17 18 19 
       "th", "th", "th", "th", "th", "th", "th", "tn", "th", "th", 
       // 20 21 22 23 24 25 26 27 28 29 
       "th", "st", "nd", "rd", "th", "th", "th", "tn", "th", "th", 
       // 30 31 
       "th", "st" 
      }; 
     public static string ToSpecialString(this DateTime dt) 
     { 
      string s = dt.ToString(" MMMM yyyy"); 
      string t = string.Format("{0}{1}", dt.Day, extensions[dt.Day]); 
      return t + s; 
     } 
} 

Test/Uso Ti piace questa:

Console.WriteLine(DateTime.Now.ToSpecialString()); 
Console.WriteLine(new DateTime(1990, 11, 12).ToSpecialString()); 
Console.WriteLine(new DateTime(1990, 1, 1).ToSpecialString()); 
Console.WriteLine(new DateTime(1990, 1, 2).ToSpecialString()); 
Console.WriteLine(new DateTime(1990, 1, 3).ToSpecialString()); 
Console.WriteLine(new DateTime(1990, 1, 4).ToSpecialString()); 
Console.WriteLine(new DateTime(1990, 12, 15).ToSpecialString()); 
Console.WriteLine(new DateTime(1990, 8, 19).ToSpecialString()); 
Console.WriteLine(new DateTime(1990, 9, 22).ToSpecialString()); 
Console.ReadKey(); 

Speranza che aiuta.

0

ho seguito la stringa di esempio blog da JSL vscontrol e ha un bug, alla fine si è dimenticato di concatenare il numero del giorno all'inizio della riga e quindi dovrebbe essere

return strNum + ordinal + str; 

e non!

return ordinal + str; 
+0

private static string Ordinal (DateTime date) { int dayvalue = Convert.ToInt32 (data.Giorno); string strNum = dayvalue.ToString(); string ordinal = string.Empty; se (strNum.EndsWith ("0") || strNum.EndsWith ("4") || strNum.EndsWith ("5") || strNum.EndsWith ("6") || strNum.EndsWith ("7") || strNum.EndsWith ("8") || strNum.EndsWith ("9")) { ordinale = "th"; } else if (strNum.EndsWith ("1")) { ordinale = "st"; } else if (strNum.EndsWith ("2")) { ordinale = "nd"; } altro { ordinale = "rd"; } string str = date.ToString ("MMMMM yyyy"); return ordinal + str; } –