如何使 DateTime 独立于当前文化?
我尝试将日期时间转换为字符串并返回,但使其适用于所有文化。
我基本上有一个文本框(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
DateTime.Parse
或DateTime.ParseExact
方法,而不是使用Convert.ToDateTime
方法。两者都允许您传递一种文化,告诉您您期望如何格式化日期。DateTime.ParseExact
方法还允许您指定所需的格式,因此您可以使用此方法或多或少地解析任何格式。编辑:
关于Convert.ToDateTime。该文档表示解析时使用当前区域性: http://msdn.microsoft .com/en-us/library/xhz1w05e.aspx
可以使用 System.Threading.Thread.CurrentThread.CurrentCulture 属性找到当前区域性。
编辑2:
哦。如果您不确定给定的格式是否无效,您可能还需要使用
DateTime.TryParse
和DateTime.TryParseExact
。编辑3:
这里有很多编辑...我看到您想要确定与用户输入的日期匹配的区域性字符串。没有保证在这里起作用的通用解决方案。例如,假设用户输入了日期 01.02.11。无法确定该日期是否为日.月.年、月.日.年或年.月.日等。
您能做的最好的事情就是列出预期的输入文化,并从最有可能的文化开始,并尝试使用它来解析日期。如果失败了,你可以尝试第二个最有可能的,依此类推......
但确实不推荐这样做。要么为用户提供预期的格式,要么使用日期输入框更好,以确保您以适当的格式收到所选日期。
Instead of using the
Convert.ToDateTime
method you can use theDateTime.Parse
orDateTime.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.aspxThe current culture can be found using the
System.Threading.Thread.CurrentThread.CurrentCulture
property.Edit2:
Oh. You may also want to use
DateTime.TryParse
andDateTime.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.
Convert.ToDateTime
方法将调用DateTime.Parse
使用当前区域性 (CultureInfo.Current
) 来解析字符串。您可以在解析字符串时指定区域性。示例:
您可以使用
DateTime.ParseExact
(或DateTime.TryParseExact
)来解析使用自定义日期格式的字符串。例子:The
Convert.ToDateTime
method will callDateTime.Parse
to parse the string, using the current culture (CultureInfo.Current
).You can specify a culture when parsing the string. Example:
You can use
DateTime.ParseExact
(orDateTime.TryParseExact
) to parse the string using a custom date format. Example:另一个解决方案:
Another solution :