如何日期转换为Word格式? [英] How to convert date to word format?
问题描述
我有一个约会一样12/05/2012现在我想改变这种格式,以简单的字符串。
为前。
字符串newdate =新的字符串();
newdate =12/05/2012;
日期时间Bdate = DateTime.ParseExact(Newdate,DD / MM / YYYY,System.Globalization.CultureInfo.InvariantCulture);
现在我BDate是的DateTime
IE浏览器。 BDate = 2012/05/12
现在我想要做这样的事情。
如果我Bdate是12/05/2012
所以我想一个字符串,如十二五月二○一二年
我怎样才能做到这一点?
请帮我...
在此先感谢....
您需要看每个日期部分,用一个函数来获取书面等价的。我已经包括低于一类整数转换为书面文本,并扩展以支持的DateTime
的转换,以及:
公共静态类WrittenNumerics
{
静态只读的String []的人=新的String [] {,一,二,三,四有,五个一,六个一,七,八,九 };
静态只读的String []青少年=新的String [] {十,十一,十二,十三,十四,十五,十六,十七,十八,古诗十九首};
静态只读的String [] =数万新的字符串[] {二,三,四十,五十,六十,七十,八十,九};
静态只读的String [] = thousandsGroups {,千,万,亿}; 私人静态字符串FriendlyInteger(INT N,串leftDigits,诠释千)
{
如果(N == 0)
返回leftDigits; 字符串friendlyInt = leftDigits;
如果(friendlyInt.Length大于0)
friendlyInt + =; 如果(正小于10)
friendlyInt + =的人[N];
否则如果(N小于20)
friendlyInt + =青少年[N - 10];
否则如果(N小于100)
friendlyInt + = FriendlyInteger(N%10,数十[N / 10 - 2],0);
否则如果(N下; 1000)
friendlyInt + = FriendlyInteger(N%100,(那些[N / 100] +百),0);
其他
friendlyInt + = FriendlyInteger(N%1000,FriendlyInteger(N / 1000,,数千+ 1),0); 返回friendlyInt + thousandsGroups [上千]
} 公共静态字符串DateToWritten(DateTime的日期)
{
返回的String.Format({0} {1} {2},IntegerToWritten(date.Day),与Date.toString(MMMM),IntegerToWritten(date.Year));
} 公共静态字符串IntegerToWritten(INT N)
{
如果(N == 0)
返回零;
否则如果(正℃,)
回到负+ IntegerToWritten(-N); 返回FriendlyInteger(正,,0);
}
}
<分> 声明:基本功能的 @Wedge <礼遇/ p>
块引用>使用这个类,只需调用DateToWritten方式:
VAR输出= WrittenNumerics.DateToWritten(DateTime.Today);
以上的输出是:
五月十二千二十二
I have one date like 12/05/2012 now i would like to change that format in to simple string.
for ex.
string newdate = new string(); newdate = "12/05/2012"; DateTime Bdate = DateTime.ParseExact(Newdate, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
now my BDate is
DateTime
ie.BDate= 2012/05/12
now i want to do something like
if my Bdate is 12/05/2012 so i want a string which is similar like "Twelve May two thousand twelve"
How can i do this?
Please help me...
Thanks in advance....
解决方案You'll need to look at each date part and use a function to get the written equivalent. I've included a class below that converts integers to written text, and extended it to support
DateTime
conversion as well:public static class WrittenNumerics { static readonly string[] ones = new string[] { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" }; static readonly string[] teens = new string[] { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" }; static readonly string[] tens = new string[] { "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" }; static readonly string[] thousandsGroups = { "", " Thousand", " Million", " Billion" }; private static string FriendlyInteger(int n, string leftDigits, int thousands) { if (n == 0) return leftDigits; string friendlyInt = leftDigits; if (friendlyInt.Length > 0) friendlyInt += " "; if (n < 10) friendlyInt += ones[n]; else if (n < 20) friendlyInt += teens[n - 10]; else if (n < 100) friendlyInt += FriendlyInteger(n % 10, tens[n / 10 - 2], 0); else if (n < 1000) friendlyInt += FriendlyInteger(n % 100, (ones[n / 100] + " Hundred"), 0); else friendlyInt += FriendlyInteger(n % 1000, FriendlyInteger(n / 1000, "", thousands + 1), 0); return friendlyInt + thousandsGroups[thousands]; } public static string DateToWritten(DateTime date) { return string.Format("{0} {1} {2}", IntegerToWritten(date.Day), date.ToString("MMMM"), IntegerToWritten(date.Year)); } public static string IntegerToWritten(int n) { if (n == 0) return "Zero"; else if (n < 0) return "Negative " + IntegerToWritten(-n); return FriendlyInteger(n, "", 0); } }
Disclaimer: Basic functionality courtesy of @Wedge
Using this class, just call the DateToWritten method:
var output = WrittenNumerics.DateToWritten(DateTime.Today);
The output of the above is:
Twelve May Two Thousand Twelve
这篇关于如何日期转换为Word格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!