我有关于字符串转换的问题.我有一个文本框,用户将输入这种格式的日期(年/月/日).
现在我必须转换它,因此它是MySQL友好的.
到目前为止,这就是我所做的
currentExpDate = txtDateStore.txt; //(i.e 25/12/13)
MessageBox.Show(currentExpDate.ToString()); // for debugging
//DateTime dt = DateTime.Parse(currentExpDate);
DateTime dt = DateTime.ParseExact(
currentExpDate,
"dd/MM/yyyy",
CultureInfo.InvariantCulture);
string mySQLDate = dt.ToString("yyyy-MM-dd");
Run Code Online (Sandbox Code Playgroud)
每当我尝试解析它时,它总是会抛出一个错误.我得到字符串异常,说字符串格式是它无法识别的格式.
如果我尝试用这种格式输入日期dd/mm/yyyy就像魅力一样.有没有解决方法可以解决这个问题?
谢谢
您应该直接将日期用作DateTime存储在数据库中.没有理由将其转换回字符串.