-2
System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern="DD/MM/YYYY";
System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortTimePattern ="hh:mm tt";

I am overriding the Date and time format for a culture in thread by doing this we will get Date and Time in given format in DateTime.Now.

I am able to get preferred format for Date same thing not working for Time.

How to get time in preferred format using above culture threading.

Prisoner
  • 1,839
  • 2
  • 22
  • 38

1 Answers1

0

You might need to create an object of type 'System.Globalization.CultureInfo' and set the date and time format specifications on that object.

Next you need to set the current culture of your thread to that culture.

I have given the code for your reference below

private void UpdateCurrentCulture()
    {
        System.Globalization.CultureInfo objCulture = new System.Globalization.CultureInfo("en-US");

        objCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
        objCulture.DateTimeFormat.ShortTimePattern = "hh:mm tt";

        System.Threading.Thread.CurrentThread.CurrentCulture = objCulture;
        System.Threading.Thread.CurrentThread.CurrentUICulture = objCulture;

        Console.WriteLine(DateTime.Now.ToShortDateString());
        Console.WriteLine(DateTime.Now.ToShortTimeString());
    }
Dinny
  • 455
  • 2
  • 5
  • 17