适用于所有文化的最佳日期时间转换器
我即将创建一个转换器来将日期转换为自定义格式。
事实上,在一个转换器中,我有一个像这样的日期 17/05/2011 00:00:00 我想将其转换为简单的 17/05/2011。但文化也应考虑在内。
在上面的示例中,我有英国文化,但如果用户是美国人,则应将其转换为 05/17/2011。
实际上,我还需要一个 ConvertBack 将日期转换回(无论文化如何)转换为数据库友好的格式,以便将其普遍且安全地保存回来,而不会产生误解。
这是我的第一次尝试,但已经失败了:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value == null)
return null;
DateTime date = DateTime.Parse(value.ToString(), culture);
return date;
}
同样令人惊讶的是,上面的 CultureInfo 参数是 Us-en,尽管 Windows 中的所有内容都设置为 United Kingdom。顺便说一句,我在 Silverlight 4 中使用这个转换器,也许这里获得的文化是不同的...尽管如此,我还是很奇怪,我得到了一个期望“字符串未被识别为有效的日期时间”。我认为这是由于我那里的美国文化不理解 17 是一个月。如何在 silverlight 应用程序中设置本地文化?
然后我需要第二个转换器来仅返回与上面类似的时间。我想我也许可以使用重载 DateTimeStyles.NoCurrentDateDefault 来仅从日期生成时间,对吗?
非常感谢您的帮助,
I am about to create a Converter to translate the date into a custom format.
In fact in one converter I have a date like this 17/05/2011 00:00:00 that I would like to convert to simply 17/05/2011. However the culture shall be also taken into consideration.
In my example above I had the British culture, but if the user is American then it shall be converted into 05/17/2011.
Effectively I would also need a ConvertBack to convert the date back regardless of the culture into a Database friendly format to save it back universally and safe without misunderstanding.
This was my first try which already fails:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value == null)
return null;
DateTime date = DateTime.Parse(value.ToString(), culture);
return date;
}
Also surprising is that the CultureInfo parameter above is Us-en, despite everything in Windows is set to United Kingdom. Btw I am using this converter for Silverlight 4, perhaps obtaining the culture is different here... None the less weird enough I get an expection 'String was not recognized as a valid DateTime.' I think this is due the American culture I have there that doesn't understand 17 as a month. How do I set my local culture in a silverlight app?
And then I would need a second converter to return only the time similar to above. i figured I might be able to use the overload DateTimeStyles.NoCurrentDateDefault for it to generate only time from dates, correct?
Many Thanks for your help,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置区域性可以在当前线程上完成,应用程序中的每个线程都可以处于不同的区域性中。
http://msdn。 microsoft.com/en-us/library/system.threading.thread.currentculture(v=VS.100).aspx
好奇,如果您的计算机是 en-gb 但您的应用程序是 en-us,我会推测应用程序可能通过配置文件明确地放入该文化中。如果应用程序没有被赋予文化,它将采用操作系统文化。
在 ASP.NET Web 应用程序中,我记得您还可以在配置文件中指定区域性。
您的最终目标只是切断
DateTime
的时间部分吗?如果是这样,则有一个DateTime.Date
属性可用,然后您可以使用默认的 .NET 区域性支持进行转换。ConvertBack
方法可以将时间保存为 UTC,DateTime
有多种方法可以将时间保存为 UTC。 UTC 包含时区信息并采用一种格式,有利于数据库。http://msdn.microsoft.com/en-us/library /system.datetime.touniversaltime.aspx
不过说实话,如果 SQL Server 是您的数据库服务器,只要您将日期存储为日期而不是字符串,那么文化就会再次为您处理。
Setting a culture can be done on the current thread, each thread in an application can be in a different culture.
http://msdn.microsoft.com/en-us/library/system.threading.thread.currentculture(v=VS.100).aspx
Curious, if your computer is in en-gb but your application is in en-us, I would surmise the application is being put into that culture explicitly, perhaps by the configuration file. If the application is not given a culture, it takes the OS culture.
In ASP.NET web apps I remember you could also specify culture in the config file.
Is your end goal just to cut off the time portion of the
DateTime
? If so, there is aDateTime.Date
property available and you could then convert using the default .NET culture support.The
ConvertBack
method could save the time as UTC, there are various methods on aDateTime
for saving to UTC. UTC includes time zone information and comes in one format, good for databases.http://msdn.microsoft.com/en-us/library/system.datetime.touniversaltime.aspx
Though to be honest, if SQL Server is your database server, so long as you store dates as dates and not strings, again culture is taken care of for you.