带有 CultureInfo 的 ASP.Net DateTime
一个常见问题:
DateTime 对象是否存储 CultureInfo,或者您需要使用 Formatter 根据当前区域性来格式化 DateTime?
我有一个返回 DateTime 的类属性。在该属性中,我使用 CultureInfo 对象设置具有当前区域性信息的 DateTime 对象。下面是我正在使用的类属性的代码:
public DateTime PrintedQuoteDate {
get {
DateTime printQuoteDate = DateTime.Today;
// cInfo = CultureInfo object
return Convert.ToDateTime(printQuoteDate , cInfo);
}
}
所以我的问题是,当我在代码中使用上述属性时,它是否具有我在其 get 方法中设置的相应区域性信息,或者我必须使用相同的区域性信息用于格式化日期时间的 CONVERT 代码。这里的限制是该属性只能返回 DateTime 类型。
有什么想法、建议
One general question:
Does the DateTime object stores the CultureInfo with it, or you need to use the Formatter to format the DateTime according to current culture ?
I have a class property that retuns a DateTime. Within that property I am setting the DateTime object with current culture information using CultureInfo object. Below is the code for class property I am using:
public DateTime PrintedQuoteDate {
get {
DateTime printQuoteDate = DateTime.Today;
// cInfo = CultureInfo object
return Convert.ToDateTime(printQuoteDate , cInfo);
}
}
So my question is when I will use the above property in my code, will it have the corrosponding culture information that I am setting in its get method, or I will have to use the same CONVERT code for formatting date time. The restriction here is that the Property should return only DateTime type.
Any idea, suggestions
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DateTime
不存储任何文化。事实上,它甚至不保存对 TimeZone 的引用,它只知道它是否是 UTCDateTime
。这是由内部枚举处理的。在使用
DateTime
的 ToString 方法时,您需要指定一个格式提供程序(每个区域性本身就是一个格式提供程序),否则它将使用以下区域性(真正的区域性而不是 UI 区域性)当前线程。您可以通过使用 ISO 国家/地区代码来获得预定义的文化,如下所示:
正如您所见,对于丹麦语,指定语言就足够了,因为没有其他语言环境(据我所知)。
DateTime
does not store any Culture what so ever. In fact it does not even hold a reference to a TimeZone, all it knows is whether it is a UTCDateTime
or not. This is handled by an internal enum.You need to specify a format provider (every culture in itself is a format provider) when using the ToString method of a
DateTime
, otherwise it will use the culture (really the culture and not the UI Culture) of the current thread.You can get a predifined culture by using the ISO country/locale codes like this:
As you can see for danish it is enough to specify the language since there are no other locales (to my knowledge).