我怎样才能获得基于文化信息的日期和时间格式? [英] How can I get date and time formats based on Culture Info?
问题描述
我要的是..
如果文化为en-US然后
What I want is.. If culture is en-US then
string dateFormat="MM/dd/yyyy";
string timeFormat="24.00 hrs";
如果文化是EN-GB然后
If culture is en-GB then
string dateFormat="dd/mmyyyy";
string timeFormat="24.00 hrs";
等其他国家。
现在我怎么得到这些日期和时间格式值?什么是标准?像所有国家使用类似的日期/时间格式,哪些没有?
Now how do I get these date and time format values ? What are the standards? Like which all countries use similar date/time formats and which ones don't ?
确定我尝试这样做: -
DateTime myDate = new DateTime();
string us = myDate.ToString(new CultureInfo("en-US"));
我们的字符串值,得到= 1/1/0001 12:00:00 AM
string us gets value =1/1/0001 12:00:00 AM
现在我如何提取DD / MM / YYYY和24.00小时出了这......在我的表我DATEFORMAT列...我想存储字符串,如DD / MM / YYYY或MM / DD / YYYY表中未dates..In我的时间格式列中的值是商店STRINGS太像我需要存储或者24:00hrs或12:00hrs
Now how do I extract "dd/mm/yyyy" and "24.00 hrs" out of this...in my Dateformat column in my Table... I want to store STRINGS such as dd/mm/yyyy or mm/dd/yyyy NOT dates..In my TimeFormat column in the table, the values to be stores are STRINGS too, like I need to store either "24:00hrs" or "12:00hrs"
我现在该如何做到这一点?
How do I do this now ?
**使用ShorTimePattern返回这些值
**using ShorTimePattern returns these values as
h:mm tt and HH:mm
如果我想在我的数据库存储值正是因为24:00hrs和12:00hrs,我怎么使用这些values..h:毫米TT和HH:MM
其中之一是24小时格式和12小时的格式?**
If I want to store the values in my DB exactly as "24:00hrs" and "12:00hrs", how do I use these values..h:mm tt and HH:mm which one is for 24 hr format and which for 12 hr format ?**
确定,现在还有另外一个问题太...我想了解小数分隔符和千位分隔符太基础上CultureInfo的信息......什么的财产是什么?
推荐答案
您可以检索从的CultureInfo
DateTimeFormat $ c中的格式字符串$ C>属性,它是一个<一个href=\"http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx\"><$c$c>DateTimeFormatInfo$c$c>实例。这反过来有类似性质
ShortDatePattern
和 ShortTimePattern
,包含格式字符串:
You can retrieve the format strings from the CultureInfo
DateTimeFormat
property, which is a DateTimeFormatInfo
instance. This in turn has properties like ShortDatePattern
and ShortTimePattern
, containing the format strings:
CultureInfo us = new CultureInfo("en-US");
string shortUsDateFormatString = us.DateTimeFormat.ShortDatePattern;
string shortUsTimeFormatString = us.DateTimeFormat.ShortTimePattern;
CultureInfo uk = new CultureInfo("en-GB");
string shortUkDateFormatString = uk.DateTimeFormat.ShortDatePattern;
string shortUkTimeFormatString = uk.DateTimeFormat.ShortTimePattern;
如果你只是想使用格式化日期/时间的CultureInfo
,在把它作为你的 IFormatter
时转换的DateTime
为字符串,使用 的ToString
方式:
If you simply want to format the date/time using the CultureInfo
, pass it in as your IFormatter
when converting the DateTime
to a string, using the ToString
method:
string us = myDate.ToString(new CultureInfo("en-US"));
string uk = myDate.ToString(new CultureInfo("en-GB"));
这篇关于我怎样才能获得基于文化信息的日期和时间格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!