How to get System DateTime format?

后端 未结 3 1918
梦谈多话
梦谈多话 2020-12-13 13:55

I am looking for a solution to get system date time format.

For example: if I get DateTime.Now? Which Date Time Format is this using? DD/MM/YYYY

3条回答
  •  既然无缘
    2020-12-13 14:42

    The answers above are not fully correct.

    I had a situation that my Main thread and my UI thread were forced to be in "en-US" culture (by design). My Windows DateTime format was "dd/MM/yyyy"

    string sysFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
    string sysUIFormat = CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern;
    

    returned "MM/dd/yyyy", but I wanted to get my real Windows format. The only way I was able to do so is by creating a dummy thread.

    System.Threading.Thread threadForCulture = new System.Threading.Thread(delegate(){} );
    string format = threadForCulture.CurrentCulture.DateTimeFormat.ShortDatePattern;
    

提交回复
热议问题