且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C#中将datetimepicker值更改为单词?

更新时间:2023-02-12 10:52:49

检查此

 使用系统; 

命名空间 ConsoleApplication1
{
class 计划
{
静态 void Main( string [] args)
{
string [] day = { 首先 Second 第三个 Fourth, Fifth 第六个 第七 第八个 第九个 第十个
第十一个 Tweleveth 第十三 第十四次 第十五 Sixteeth 第十七 第十八, 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 [] { 一个 两个 三个 四个
十一 Tweleve 十三 十四 十五 十六 十七 十八 Ninteen };
var tensMap = new [] { 二十 三十 四十 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# - 将数字转换为文本(字) [ ^ ]



,你扩展该代码以处理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]