如何使 DateTime 独立于当前文化?

发布于 2024-11-05 08:13:20 字数 516 浏览 0 评论 0原文

我尝试将日期时间转换为字符串并返回,但使其适用于所有文化。

我基本上有一个文本框(tbDateTime)和一个标签(lbDateTime)。该标签告诉用户,软件期望以哪种格式输入 tbDateTime。文本框的输入将用于 MySQL 命令。

目前它的工作原理如下:

lbDateTime.Text = "DD.MM.YYYY hh:mm:ss";   // I live in germany
DateTime date = Convert.ToDateTime(tbDateTime.Text);
String filter = date.ToString("yyyy-MM-dd HH:mm:ss");

现在我的问题:

  • 是否可以根据当前区域性确定 lbDateTime.Text 的格式字符串?
  • Convert.ToDateTime 函数使用哪种格式?

我希望你能帮助我。我这里实际上没有电脑来测试不同的文化,所以我很担心我会做错什么。

I cam trying to convert a datetime to string and back, but making it so that it works for all cultures.

I basically have a Textbox (tbDateTime) and a label (lbDateTime). The label tells the user, in which format the software expects the input of tbDateTime. The input of the Textbox will be used for an MySQL command.

Currently it works like this:

lbDateTime.Text = "DD.MM.YYYY hh:mm:ss";   // I live in germany
DateTime date = Convert.ToDateTime(tbDateTime.Text);
String filter = date.ToString("yyyy-MM-dd HH:mm:ss");

Now my question:

  • Is it possible to determine the format-string for lbDateTime.Text based on the current culture?
  • Which format does the Convert.ToDateTime function uses?

I hope you can help me. I have actually no pc here to test different cultures, so I'm very afraid that I make something wrong.

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

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

发布评论

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

评论(3

时光与爱终年不遇 2024-11-12 08:13:20

您可以使用 DateTime.ParseDateTime.ParseExact 方法,而不是使用 Convert.ToDateTime 方法。两者都允许您传递一种文化,告诉您您期望如何格式化日期。

DateTime.ParseExact 方法还允许您指定所需的格式,因此您可以使用此方法或多或少地解析任何格式。

编辑:
关于Convert.ToDateTime。该文档表示解析时使用当前区域性: http://msdn.microsoft .com/en-us/library/xhz1w05e.aspx

可以使用 System.Threading.Thread.CurrentThread.CurrentCulture 属性找到当前区域性。

编辑2:
哦。如果您不确定给定的格式是否无效,您可能还需要使用 DateTime.TryParseDateTime.TryParseExact

编辑3:
这里有很多编辑...我看到您想要确定与用户输入的日期匹配的区域性字符串。没有保证在这里起作用的通用解决方案。例如,假设用户输入了日期 01.02.11。无法确定该日期是否为日.月.年、月.日.年或年.月.日等。

您能做的最好的事情就是列出预期的输入文化,并从最有可能的文化开始,并尝试使用它来解析日期。如果失败了,你可以尝试第二个最有可能的,依此类推......

但确实不推荐这样做。要么为用户提供预期的格式,要么使用日期输入框更好,以确保您以适当的格式收到所选日期。

Instead of using the Convert.ToDateTime method you can use the DateTime.Parse or DateTime.ParseExact methods. Both allow you to pass a culture that tells how you expect the date to be formatted.

The DateTime.ParseExact method also allows you to specify the format you expect, so you can parse more or less any format with this method.

Edit:
Regarding Convert.ToDateTime. The documentation says that the current culture is used when parsing: http://msdn.microsoft.com/en-us/library/xhz1w05e.aspx

The current culture can be found using the System.Threading.Thread.CurrentThread.CurrentCulture property.

Edit2:
Oh. You may also want to use DateTime.TryParse and DateTime.TryParseExact if you are unsure whether the given format is invalid.

Edit3:
Lots of edits here... I see that you want to determine the culture string that matches the date the user has entered. There is no general solution that is guaranteed to work here. Say for instance that the user has entered the date 01.02.11. There is no way to be certain if this date is in day.month.year or month.day.year or year.month.day and so on.

The best you can do is to have a list of expected input cultures and start with the most likely and try to parse the date using that. If that fails, you can try the second most likely and so on...

But this is really not recommended. Either give the user an expected format, or better, use a date input box that ensures that you receive the selected date in an appropriate format.

倾其所爱 2024-11-12 08:13:20

Convert.ToDateTime 方法将调用 DateTime.Parse 使用当前区域性 (CultureInfo.Current) 来解析字符串。

您可以在解析字符串时指定区域性。示例:

DateTime data = DateTime.Parse(tbDateTime.Text, new CultureInfo("en-GB"));

您可以使用 DateTime.ParseExact (或 DateTime.TryParseExact)来解析使用自定义日期格式的字符串。例子:

DateTime data = DateTime.ParseExact(tbDateTime.Text, "dd'.'MM'.'yyyy HH':'mm':'ss", CultureInfo.InvariantCulture);

The Convert.ToDateTime method will call DateTime.Parse to parse the string, using the current culture (CultureInfo.Current).

You can specify a culture when parsing the string. Example:

DateTime data = DateTime.Parse(tbDateTime.Text, new CultureInfo("en-GB"));

You can use DateTime.ParseExact (or DateTime.TryParseExact) to parse the string using a custom date format. Example:

DateTime data = DateTime.ParseExact(tbDateTime.Text, "dd'.'MM'.'yyyy HH':'mm':'ss", CultureInfo.InvariantCulture);
恏ㄋ傷疤忘ㄋ疼 2024-11-12 08:13:20

另一个解决方案:

// Specify the current language (used in the current system you are working on)
CultureInfo currentCulture = CultureInfo.GetCultureInfo(CultureInfo.CurrentCulture.ToString());
// Specify the language that we need
CultureInfo myLanguage = CultureInfo.GetCultureInfo("en-US");

// Adapt the DateTime here, we will use the current time for this example
DateTime currentDate = DateTime.Now;
// The date in the format that we need
string myDate = DateTime.Parse(currentDate.ToString(), currentCulture).ToString(myLanguage);

Another solution :

// Specify the current language (used in the current system you are working on)
CultureInfo currentCulture = CultureInfo.GetCultureInfo(CultureInfo.CurrentCulture.ToString());
// Specify the language that we need
CultureInfo myLanguage = CultureInfo.GetCultureInfo("en-US");

// Adapt the DateTime here, we will use the current time for this example
DateTime currentDate = DateTime.Now;
// The date in the format that we need
string myDate = DateTime.Parse(currentDate.ToString(), currentCulture).ToString(myLanguage);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文