Hello everyone, today I will show you how to set datetime format in wpf application. To set the common format for the whole app, we find the Application_Startup function in the App.xaml.cs file. Below is the code that I will introduce today:

private void Application_Startup(object sender, StartupEventArgs e)
        {
            System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("ja-JP");
            System.Globalization.DateTimeFormatInfo dateTimeInfo =
               new System.Globalization.DateTimeFormatInfo();
            // Defining various date and time formats.
            dateTimeInfo.DateSeparator = "/";
            dateTimeInfo.LongDatePattern = "yyyy.MM.dd";
            dateTimeInfo.ShortDatePattern = "yyyy.MM.dd";
            dateTimeInfo.LongTimePattern = "H:mm:ss";
            dateTimeInfo.ShortTimePattern = "H:mm";
            // Setting application wide date time format.
            ci.DateTimeFormat = dateTimeInfo;
            System.Threading.Thread.CurrentThread.CurrentCulture = ci;
            System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
            System.Globalization.CultureInfo.DefaultThreadCurrentCulture = ci;
            System.Globalization.CultureInfo.DefaultThreadCurrentUICulture = ci;
        }

In the above code:

System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("ja-JP");

In this paragraph, we will set the CultureInfo that you want to apply to the app, it can be ja-JP, en-US....

 dateTimeInfo.DateSeparator = "/";
            dateTimeInfo.LongDatePattern = "yyyy.MM.dd";
            dateTimeInfo.ShortDatePattern = "yyyy.MM.dd";
            dateTimeInfo.LongTimePattern = "H:mm:ss";
            dateTimeInfo.ShortTimePattern = "H:mm";

This paragraph is the datetime format that you want to use for the whole app.

System.Threading.Thread.CurrentThread.CurrentCulture = ci;
            System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
            System.Globalization.CultureInfo.DefaultThreadCurrentCulture = ci;
            System.Globalization.CultureInfo.DefaultThreadCurrentUICulture = ci;

In this paragraph, we will assign the datetime format we want to all threads.

Today's content I would like to end here. See you in the next posts.