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
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.
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"));
GetDateTimeFormats can parse DateTime to different formats. Example to "yyyy-MM-dd" format.
SomeDate.Value.GetDateTimeFormats()[5]
GetDateTimeFormats
We can use the below its very simple.
Date.ToString("yyyy-MM-dd");
Use DateTime.Now.ToString("yyyy-MM-dd h:mm tt");
. See this.