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

前端 未结 5 1222
难免孤独
难免孤独 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

    Have you tried?

    var isoDateTimeFormat = CultureInfo.InvariantCulture.DateTimeFormat;
    
    // "2013-10-10T22:10:00"
     dateValue.ToString(isoDateTimeFormat.SortableDateTimePattern); 
    
    // "2013-10-10 22:10:00Z"    
    dateValue.ToString(isoDateTimeFormat.UniversalSortableDateTimePattern)
    

    Also try using parameters when you store the c# datetime value in the mySql database, this might help.

    0 讨论(0)
  • 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"));
    
    0 讨论(0)
  • 2021-02-01 13:25

    GetDateTimeFormats can parse DateTime to different formats. Example to "yyyy-MM-dd" format.

    SomeDate.Value.GetDateTimeFormats()[5]
    

    GetDateTimeFormats

    0 讨论(0)
  • 2021-02-01 13:34

    We can use the below its very simple.

    Date.ToString("yyyy-MM-dd");
    
    0 讨论(0)
  • 2021-02-01 13:36

    Use DateTime.Now.ToString("yyyy-MM-dd h:mm tt");. See this.

    0 讨论(0)
提交回复
热议问题