如何在C#中将datetimepicker值更改为单词? [英] How to change datetimepicker value into words in C#?
本文介绍了如何在C#中将datetimepicker值更改为单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮助我,
我想将日期值更改为单词,例如: -
将此转换为22/08 / 2002年进入这个八月二十二日两千两个
请给我解决方案或一些想法。
我尝试过:
我试过这个
lbldobinwords .Text = String.Format({0:dd MMMM yyyy},dtpdob.Value);
但这给了我这样的输出2002年8月22日 。
解决方案
检查此
使用系统;
命名空间 ConsoleApplication1
{
class 计划
{
静态 void Main( string [] args)
{
string [] day = { 首先, Second, 第三个, < span class =code-string> Fourth, Fifth, 第六个, 第七, 第八个, 第九个, 第十个,
第十一个, Tweleveth, 第十三, 第十四次, 第十五, Sixteeth, 第十七, < span class =code-string>第十八, Ninteenth, secondth,
Twenty First, Twenty Second , 二十三, 第二十四, Twenty Fifth , Twenty Sixth, Twenty Seventh, Twenty Eighth, Twenty Ninth, 第30个, Thirty First};
DateTime dateValue = DateTime.Now;
string month = String .Format( {0:MMMM},dateValue);
string year = ConvertNumbertoWords(dateValue.Year);
string inWords = string .Format( {0} {1} {2},day [dateValue.Day - 1 ],月,年);
}
public static string ConvertNumbertoWords( int number)
{
if (number == 0 )
return 零;
if (number < 0 )
return minus + ConvertNumbertoWords(Math.Abs(number));
string words = ;
if ((number / 1000 )> 0 )
{
words + = ConvertNumbertoWords(number / 1000 )+ 千;
number%= 1000 ;
}
if ((number / 100 )> 0 )
{
words + = ConvertNumbertoWords(number / 100 )+ 百;
number%= 100 ;
}
如果(数字> 0 )
{
if (words!= )
words + = 和跨度>;
var unitsMap = new [] { 零, 一个, 两个, 三个, 四个,< span class =code-string> 五, 六, 七, 八, 九, 十, 十一, Tweleve, 十三, 十四, 十五, 十六, 十七, 十八, Ninteen };
var tensMap = new [] { 零, 十, 二十, 三十, 四十,< span class =code-string> Fifty, 六十, 七十, 八十, 九十};
if (number < 20 )
words + = unitsMap [number];
else
{
words + = tensMap [number / 10 ]。
if ((编号% 10 )> ; 0 )
words + = + unitsMap [number% 10 ];
}
}
返回字;
}
}
}
你真的需要得到日期的数字成分,例如
22
08 br / >
2002
并通过类似的方式运行它们 NumberText / NumberText.cs at master·robertgreiner / NumberText·GitHub [ ^ ]或此 C# - 将数字转换为文本(字) [<小时ef =http://www.softwareandfinance.com/CSharp/Number_To_Text.htmltarget =_ blanktitle =新窗口> ^ ]
,你扩展该代码以处理08 - >八月
做谷歌'c#转换数字文字'你会看到很多其他方法 - 你甚至不需要大范围的数字你只做几天,几年,几年
Humanizer GitHub - Humanizr / Humanizer:Humanizer满足您操作和显示字符串,枚举,日期,时间,时间跨度,数字和数量的所有.NET需求 [ ^ ]也有很多好东西。例如Too() - 你可以用它作为基础并扩展它[ /编辑]
Please help me,
I want to change date value into words for example:-
convert this "22/08/2002" into this "Twenty Second August Two thousand two"
give me solution or some idea please.
What I have tried:
I tried this
lbldobinwords.Text = String.Format("{0:dd MMMM yyyy}", dtpdob.Value);
but this gives me output like this "22 August 2002".
解决方案
Check this
using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string[] day = {"First","Second","Third","Fourth","Fifth","Sixth","Seventh","Eighth","Ninth","Tenth", "Eleventh","Tweleveth","Thirteenth","Fourteenth","Fifteenth","Sixteeth","Seventeenth","Eighteenth","Ninteenth","twentyth", "Twenty First","Twenty Second","Twenty Third","Twenty Fourth","Twenty Fifth","Twenty Sixth","Twenty Seventh","Twenty Eighth","Twenty Ninth","thirtieth","Thirty First"}; DateTime dateValue = DateTime.Now; string month = String.Format("{0:MMMM}", dateValue); string year = ConvertNumbertoWords(dateValue.Year); string inWords = string.Format("{0} {1} {2}", day[dateValue.Day - 1], month, year); } public static string ConvertNumbertoWords(int number) { if (number == 0) return "Zero"; if (number < 0) return "minus " + ConvertNumbertoWords(Math.Abs(number)); string words = ""; if ((number / 1000) > 0) { words += ConvertNumbertoWords(number / 1000) + " Thousand "; number %= 1000; } if ((number / 100) > 0) { words += ConvertNumbertoWords(number / 100) + " Hundred "; number %= 100; } if (number > 0) { if (words != "") words += "and "; var unitsMap = new[] { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Tweleve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Ninteen" }; var tensMap = new[] { "Zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" }; if (number < 20) words += unitsMap[number]; else { words += tensMap[number / 10]; if ((number % 10) > 0) words += " " + unitsMap[number % 10]; } } return words; } } }
Number Conversion used from Numbers to Words[^]
you really need to get the numeric components of the date, eg
22
08
2002
and run them through something like this NumberText/NumberText.cs at master · robertgreiner/NumberText · GitHub[^] or this C# - Convert Numbers to Text (Words)[^]
where you extend that code to handle 08 -> August
do a google for 'c# convert number text' and you'll see plenty of other ways of doing it - you dont even need a large range of numbers if you're only doing days, months, years
[edit]
Humanizer GitHub - Humanizr/Humanizer: Humanizer meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities[^] also has a lot of goodies .ToWords() for example - you could use that as a base and extend it [/edit]
这篇关于如何在C#中将datetimepicker值更改为单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文