You must Sign In to post a response.
  • Category: ASP.NET

    Date Format Using cultureinfo.invariantculture in asp.net c#

    Guys,

    I want to convert the date format MM/dd/yyyy (20/01/2016) to "JANUARY 20th" (th - prefix) format. I could convert MM/dd/yyyy (20/01/2016) to "JANUARY 20" using cultureinfo.invariantculture "MMMM d" format. But i expect the prefix (5th,3rd) with the date.

    Please help to get that required format (JANURAY 20th - th as prefix).

    Regards,
    Rajabharathi R
  • #768221
    Hi,

    Actually its called ordinal. We don't have any .NET class library to solve this issue.
    But we can use Jquery or Javascripts code for fixing this.

    We have many simple methods in online for this if you search as ordinal date.


    (DateTime.Now.Day % 10 == 1 && DateTime.Now.Day != 11) ? "st" :
    (DateTime.Now.Day % 10 == 2 && DateTime.Now.Day != 12) ? "nd":
    (DateTime.Now.Day % 10 == 3 && DateTime.Now.Day != 13) ? "rd":
    "th" //Else part where all other values will be th.


    You can create this functionality with you own ideas with Switch case, For loop or with any.

    Thanks,
    Mani

  • #768229
    There is no inbuilt namespace or a way to do it, you need to customize logic for it
    see below snippet

    extension IntegerType {
    func ordinalString() -> String {
    switch self % 10 {
    case 1...3 where 11...13 ~= self % 100: return "\(self)" + "th"
    case 1: return "\(self)" + "st"
    case 2: return "\(self)" + "nd"
    case 3: return "\(self)" + "rd"
    default: return "\(self)" + "th"
    }
    }
    }

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #768254
    Hi,

    Simple example that you can refer:

    DateTime objDate = new DateTime();
    var Day = (objDate.Day + "th").Replace("1th", "1st").Replace("2th", "2nd").Replace("3th", "3rd");

  • #768261
    CultureInfo.InvariantCulture Property is culture-independent (invariant) under the Namespace of System.Globalization. code snippet for the example of CultureInfo.InvariantCulture

    {
    StreamWriter sw = new StreamWriter(@".\ExampleDateData.dat");

    DateTime dtIn = DateTime.Now;
    CultureInfo invC = CultureInfo.InvariantCulture;
    sw.WriteLine(dtIn.ToString("r", invC));
    sw.Close();
    StreamReader sr = new StreamReader(@".\ExampleDateData.dat");
    String input;
    while ((input = sr.ReadLine()) != null)
    {
    Console.WriteLine("Stored data: {0}\n" , input);
    DateTime dtOut = DateTime.Parse(input, invC, DateTimeStyles.RoundtripKind);
    CultureInfo frFr = new CultureInfo("fr-FR");
    Console.WriteLine("Date formatted for the {0} culture: {1}" ,
    frFr.Name, dtOut.ToString("f", frFr));
    CultureInfo deDe= new CultureInfo("de-De");
    Console.WriteLine("Date formatted for {0} culture: {1}" ,
    deDe.Name, dtOut.ToString("f", deDe));
    }
    sr.Close();
    }
    }


Sign In to post your comments