Da ich keine DateTime.ToString()-Formatierung finden konnte, die mir das Datum umgangssprahclich ausgibt, habe ich sie mir eben selbst geschrieben.
Der folgende Code macht aus dem 02.02.2013 den Feb 2nd 2013.
---
Since I wasn't able to find a DateTime.ToString() to print out a date with ordinal numbers, I worte it myself.
The following code transforms 02/02/2013 into Feb 2nd 2013.
using System;
using System.Globalization;
using System.Text;
namespace EnglishOrdinals
{
public static class Program
{
private static String GetEnglishOrdinal(DateTime date)
{
String lastDayDigit;
lastDayDigit = date.Day.ToString();
lastDayDigit = lastDayDigit.Substring(lastDayDigit.Length - 1);
if ((date.Day >= 11) && (date.Day <= 13))
{
lastDayDigit = "0";
}
switch (lastDayDigit)
{
case ("1"):
{
return ("st");
}
case ("2"):
{
return ("nd");
}
case ("3"):
{
return ("rd");
}
default:
{
return ("th");
}
}
}
public static void Main()
{
DateTime date;
date = DateTime.Now;
Console.WriteLine(FormatDate(date));
for (Int32 day = 1; day < 32; day++)
{
date = new DateTime(2013, 3, day);
Console.WriteLine(FormatDate(date));
}
Console.ReadLine();
}
private static String FormatDate(DateTime date)
{
StringBuilder sb;
CultureInfo ci;
ci = CultureInfo.GetCultureInfo("en-US");
sb = new StringBuilder();
sb.Append(date.ToString("MMM d", ci));
sb.Append(GetEnglishOrdinal(date));
sb.Append(date.ToString(" yyyy", ci));
return (sb.ToString());
}
}
}
Kommentare zum Snippet