如何使DateTime独立于当前的文化? [英] How to make DateTime independent from the current culture?

查看:99
本文介绍了如何使DateTime独立于当前的文化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将日期时间转换为字符串并返回,但是为了使它适用于所有区域性。

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

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

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

现在我的问题:


  • 是否可以根据当前区域性确定lbDateTime.Text的格式字符串?

  • Convert.ToDateTime函数使用哪种格式?

我希望你能帮助我。我实际上没有电脑可以测试不同的文化,所以我很担心自己做错了。

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.

推荐答案

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

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.

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

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.

编辑:

关于 Convert.ToDateTime 。该文档说,在解析时使用了当前的区域性: http://msdn.microsoft .com / en-us / library / xhz1w05e.aspx


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

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

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

Edit2:

哦。如果不确定给定格式是否无效,则可能还需要使用 DateTime.TryParse DateTime.TryParseExact

Edit3:

这里有很多编辑...我看到您想确定与之匹配的文化字符串用户输入的日期。没有可以保证在此工作的通用解决方案。假设用户输入了日期01.02.11。无法确定此日期是否在day.month.year或month.day.year.year或year.month.day等。


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.

这篇关于如何使DateTime独立于当前的文化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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