如何使用全球化在 C# 中将 MM/dd/yyyy 转换为 yyyy/MM/dd

发布于 2024-12-23 09:43:19 字数 177 浏览 4 评论 0原文

我有一个文本框,用户应该在其中输入 MM/dd/yyyy 格式的日期。该日期在数据库中存储为 yyyy/MM/dd。

我希望用户以 MM/dd/yyyy 格式输入日期,稍后我想将其转换为 yyyy/mm/dd 以便我可以查询数据库。

如何将用户输入的日期 MM/dd/yyyy 转换为 yyyy/mm/dd?

I have a text box in which a user is supposed to enter a date in MM/dd/yyyy format. This date is stored as yyyy/MM/dd in the database.

I want the user to enter the date in MM/dd/yyyy format and later I want to convert it to yyyy/mm/dd so that I can query the database.

How can I convert the user input date MM/dd/yyyy to yyyy/mm/dd?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风筝有风,海豚有海 2024-12-30 09:43:19

如果您确定输入字符串的格式,请使用 DateTime.ParseExact 指定“MM/dd/yyyy”,然后使用 .ToString 和相应的“yyyy/MM/dd”格式字符串返回 DateTime。

无需为此引用 System.Globalization 命名空间中的任何内容。

也就是说,您的数据库应该以日期时间格式存储日期,而不是字符串,在这种情况下,格式并不重要,因为您的 DBMS 应该为您进行转换。

If you're certain of the input string's format, use DateTime.ParseExact specifying "MM/dd/yyyy", then return the DateTime using .ToString with the appropriate "yyyy/MM/dd" format string.

There's no need to reference anything in the System.Globalization namespace for this.

That said, your database should be storing dates with a datetime format, rather than a string, in which case the format doesn't matter as your DBMS should do the conversion for you.

审判长 2024-12-30 09:43:19

您可以解析日期并格式化结果:

string str = Date.Parse(myDate).ToString("yyyy/MM/dd");

或者,如果当前区域性不支持该日期格式并且您已经验证了输入:

string items[] = myDate.Split('/');
string str = items[2] + "/" + items[0] + "/" + items[1];

You can parse the date and format the result:

string str = Date.Parse(myDate).ToString("yyyy/MM/dd");

Alternatively, if the current culture doesn't support that date format and you've already validated the input:

string items[] = myDate.Split('/');
string str = items[2] + "/" + items[0] + "/" + items[1];
疧_╮線 2024-12-30 09:43:19

当你说全球化时,我假设你希望根据当前文化自动进行更改

你可以设置文化(我建议在 Global.asax.cs )

        System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("the culture you want to set"); //

你不需要接触任何日期时间的东西,它只是在你输出它们时发生。

When you said globalization, I assume you want the change to be automatic according to current culture

You can setup culture (at Global.asax.cs I suggest)

        System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("the culture you want to set"); //

you do not need to touch any datetime thing, it just happened when you output them.

尾戒 2024-12-30 09:43:19

一件事不是另一件事。

不确定为什么您要坚持输入的文本为 MM/dd/yyyy,或者为什么您没有使用日期时间选择器来确保它是日期。

但此时您获取文本框的内容作为日期,根据全球化或一组可接受的格式对其进行解析。现在它是一个日期,假设它是数据库中的日期,格式是无关紧要的,直到您使用数据库中的一些内容填充文本框,在这种情况下,您使用带有全球化参数(通常是 CultureInfo.CurrentCulture)的 DateTime 的 ToString 方法,或者如果你侥幸逃脱了 CultureInfo.InvariantCulture。
如果它是数据库中的字符串,那么这是最少的问题。

关键点是,如果正确使用日期,格式仅与 Parse 和 ToString 类型方法相关。

One thing is not the other.

Not sure why you want to insist on the text entered being MM/dd/yyyy, or why you haven't used a date time picker to make sure it is a date.

But at the point you get the content of the textbox as a date, parse it based on globalisation, or a set of acceptable formats. Now it is a date, and assuming it's a date in the database, format is irrelevant until you come to populate the text box, with some content from the DB, inwhich case you use DateTime's ToString method witha globalisation parameter, usually CultureInfo.CurrentCulture, or if you've got away with it CultureInfo.InvariantCulture.
If it's a string in the DB, then this is the least of your problems.

They key point is if you use dates properly, format is only relevant for Parse, and ToString type methods.

新雨望断虹 2024-12-30 09:43:19

我使用

IFormatProvider culture=new CultureInfo("en-GB",true);
sqlcommand cmd=new sqlcommand("query",con);
cmd.Parameters.AddWithValue("@date",DateTime.Parse(txtdate.Text.Trim(),culture,DateTimeStyles.NoCurrentDateDefault).Date);

此功能必须将格式属性设置为 dd/MM/yyyy
并且文本框只读属性必须为 false

i am using

IFormatProvider culture=new CultureInfo("en-GB",true);
sqlcommand cmd=new sqlcommand("query",con);
cmd.Parameters.AddWithValue("@date",DateTime.Parse(txtdate.Text.Trim(),culture,DateTimeStyles.NoCurrentDateDefault).Date);

for this to work the format property must be set to dd/MM/yyyy
and text box read only property must be false

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文