如何DD / MM / YYYY转换成DD / MM / YYYY [英] How to convert dd/mm/yyyy into dd/mm/yyyy

查看:181
本文介绍了如何DD / MM / YYYY转换成DD / MM / YYYY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于字符串转换的问题。
I有一个文本框,用户将输入与这种格式(日/月/年)的日期。

i have a question about string conversion. i have a textbox which the user will input with a date in this format (dd/mm/yy).

现在我必须把它转换所以它是MySQL的友好。

Now i have to convert it so it is MySQL friendly.

到目前为止,这是我做了什么。

so far this is what i have done

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");



如何过它总是抛出每当我试图解析它的错误。我得到的字符串例外,说字符串格式之一,它不承认。

How ever it will always throws an error whenever I try to parse it. I get string exception that says string format is one it does not recognize.

如果我尝试输入的日期格式DD / MM / YYYY它就像一个魅力。有任何解决方法来解决这个?

If i try to input the date with this format dd/mm/yyyy it works like a charm. Is there any workaround to solve this?

感谢

推荐答案

您应该只使用日期作为一个DateTime直接在数据库中存储。没有理由将其转换回字符串。

You should just use the date as a DateTime directly to store in your database. There is no reason to convert it back into a string.

这篇关于如何DD / MM / YYYY转换成DD / MM / YYYY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆