How to localize date format in C#

4 Answers

0 votes
// Format a date using a specific culture

using System;
using System.Globalization;

class Program
{
    static void Main()
    {
        DateTime dt = new DateTime(2026, 4, 18, 21, 30, 0);

        CultureInfo fr = new CultureInfo("fr-FR");
        CultureInfo us = new CultureInfo("en-US");
        CultureInfo de = new CultureInfo("de-DE");

        Console.WriteLine("French:      " + dt.ToString(fr));
        Console.WriteLine("US English:  " + dt.ToString(us));
        Console.WriteLine("German:      " + dt.ToString(de));
    }
}



/*
run:

French:      18/04/2026 21:30:00
US English:  4/18/2026 9:30:00 PM
German:      18.04.2026 21:30:00

*/

 



answered 6 hours ago by avibootz
0 votes
// Localized standard date formats ("d", "D", "F")

using System;
using System.Globalization;

class Program
{
    static void Main()
    {
        DateTime dt = new DateTime(2026, 4, 18, 21, 30, 0);
        CultureInfo he = new CultureInfo("en-US"); 

        Console.WriteLine("Short date (d): " + dt.ToString("d", he));
        Console.WriteLine("Long date (D):  " + dt.ToString("D", he));
        Console.WriteLine("Full date (F):  " + dt.ToString("F", he));
    }
}


/*
run:

Short date (d): 4/18/2026
Long date (D):  Saturday, April 18, 2026
Full date (F):  Saturday, April 18, 2026 9:30:00 PM

*/

 



answered 6 hours ago by avibootz
0 votes
// Use the user’s system locale (automatic localization)

using System;
using System.Globalization;

class Program
{
    static void Main()
    {
        DateTime now = DateTime.Now;

        Console.WriteLine("System locale: " + now.ToString(CultureInfo.CurrentCulture));
    }
}



/*
run:

System locale: 04/18/2026 19:11:11

*/

 



answered 6 hours ago by avibootz
0 votes
// Custom localized pattern

using System;
using System.Globalization;

class Program
{
    static void Main()
    {
        DateTime dt = new DateTime(2026, 4, 18);
        CultureInfo es = new CultureInfo("de-DE"); 

        string formatted = dt.ToString("dddd, dd MMMM yyyy", es);
        Console.WriteLine(formatted);
    }
}


/*
run:

Samstag, 18 April 2026

*/

 



answered 6 hours ago by avibootz

Related questions

1 answer 84 views
1 answer 100 views
1 answer 158 views
1 answer 106 views
1 answer 122 views
1 answer 149 views
...