c# 日期转换为单词

C#helper类将日期转换为单词,如果有兴趣的也可以自己实现汉语的版本,具体代码如下

public static class DateToWords
{
    private static CultureInfo ci = new CultureInfo("en-US");
    private static string[] unitsMap = new string[] 
      { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
                "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", 
                "Seventeen", "Eighteen", "Nineteen" };
    private static string[] tensMap = new string[] 
      { "Zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
        
    //https://stackoverflow.com/questions/2729752/converting-numbers-in-to-words-c-sharp
    private static string NumberToWords(int number)
    {
        if (number == 0)
            return "zero";
    
        string words = "";
        
        if ((number / 1000) > 0)
        {
            words += NumberToWords(number / 1000) + " Thousand ";
            number %= 1000;
        }
    
        if ((number / 100) > 0)
        {
            words += NumberToWords(number / 100) + " Hundred ";
            number %= 100;
        }
    
        if (number > 0)
        {
            if (words != "")
                words += " ";
        
            if (number < 20)
                words += unitsMap[number];
            else
            {
                words += tensMap[number / 10];
                if ((number % 10) > 0)
                    words += " " + unitsMap[number % 10];
            }
        }
    
        return Regex.Replace(words, @"\ {2,}", " ");
    }
        
    public static string Convert(DateTime dt)
    {
        //convert days
        string dtw = NumberToWords(dt.Day);
        
        //convert months
        dtw += " " + dt.ToString("MMMM", ci);    
        
        //convert years
        dtw += ", " + NumberToWords(dt.Year);
        
        return dtw;
    }
}

使用如下(控制台应用程序为例)

void Main()
{
    DateTime[] dates = new DateTime[]
        {
            new DateTime(1981, 1, 28),
            new DateTime(1998, 2, 1),
            new DateTime(2000, 3, 31),
            new DateTime(2001, 4, 8),
            new DateTime(2016, 6, 5),
            new DateTime(1977, 8, 3),
            new DateTime(2012, 10, 12),
            new DateTime(1968, 12, 15)
        };

    foreach(DateTime dt in dates)
    {
        string dtw = DateToWords.Convert(dt);
        Console.WriteLine("{0} => '{1}'", dt.ToString("dd-MM-yyyy"), dtw);
    }

    Console.ReadKey();
}

结果如下:

28-01-1981 => 'Twenty Eight January, One Thousand Nine Hundred Eighty One'
01-02-1998 => 'One February, One Thousand Nine Hundred Ninety Eight'
31-03-2000 => 'Thirty One March, Two Thousand '
08-04-2001 => 'Eight April, Two Thousand One'
05-06-2016 => 'Five June, Two Thousand Sixteen'
03-08-1977 => 'Three August, One Thousand Nine Hundred Seventy Seven'
12-10-2012 => 'Twelve October, Two Thousand Twelve'
15-12-1968 => 'Fifteen December, One Thousand Nine Hundred Sixty Eight'

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值