Convert DateTime in C# to yyyy-MM-dd format and Store it to MySql DateTime Field

前端 未结 5 1221
难免孤独
难免孤独 2021-02-01 12:49

I am trying to convert DateTime format to yyyy-MM-dd format and store it to DateTime object. But it gives me the System DateTime

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-01 13:21

    Try setting a custom CultureInfo for CurrentCulture and CurrentUICulture.

    Globalization.CultureInfo customCulture = new Globalization.CultureInfo("en-US", true);
    
    customCulture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd h:mm tt";
    
    System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
    System.Threading.Thread.CurrentThread.CurrentUICulture = customCulture;
    
    DateTime newDate = System.Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd h:mm tt"));
    

提交回复
热议问题