C#中數字日期轉中文小寫日期

來源:互聯網
上載者:User
 據客戶要求,把"2007-11-4"轉換為"二〇〇七年十一月四號"

using System;
using System.Text;
using
System.Text.RegularExpressions;

namespace AndyDateConvert
{
     
class DateConvert
      {
          private static DateConvert
m_DateConvert = null;

         private char[] strChinese;

        
private DateConvert()
         {
             strChinese = new char[]
{
                
'〇','一','二','三','四','五','六','七','八','九','十'
             };
        
}

         public static DateConvert Instance
        
{
             get
             {
                 if (m_DateConvert ==
null)
                     m_DateConvert = new
DateConvert();
                 return m_DateConvert;
            
}
         }

         public string Baodate2Chinese(string
strDate)
         {
             StringBuilder result = new
StringBuilder();

             // 依據Regex判斷參數是否正確
             Regex
theReg = new Regex(@"(d{2}|d{4})(/|-)(d{1,2})(/|-)(d{1,2})");
             if
(theReg.Match(strDate).Length != 0)
             {
                 //
將數字日期的年月日存到字元數組str中
                 string[] str = null;
                
if (strDate.Contains("-"))
                 {
                     str =
strDate.Split('-');
                 }
                 else if
(strDate.Contains("/"))
                 {
                     str =
strDate.Split('/');
                 }

                 //
str[0]中為年,將其各個字元轉換為相應的漢字
                 for (int i = 0; i <
str[0].Length; i++)
                 {
                    
result.Append(strChinese[int.Parse(str[0][i].ToString())]);
                
}
                 result.Append("年");

                 //
轉換月
                 int month = int.Parse(str[1]);
                 int
MN1 = month / 10;
                 int MN2 = month %
10;

                 if (MN1 > 1)
                
{
                     result.Append(strChinese[MN1]);
                
}
                 if (MN1 > 0)
                
{
                     result.Append(strChinese[10]);
                
}
                 if (MN2 != 0)
                
{
                     result.Append(strChinese[MN2]);
                
}
                 result.Append("月");

                 //
轉換日
                 int day = int.Parse(str[2]);
                 int DN1
= day / 10;
                 int DN2 = day % 10;

                 if
(DN1 > 1)
                 {
                    
result.Append(strChinese[DN1]);
                 }
                 if
(DN1 > 0)
                 {
                    
result.Append(strChinese[10]);
                 }
                 if (DN2
!= 0)
                 {
                    
result.Append(strChinese[DN2]);
                 }
                
result.Append("日");
             }
             else
            
{
                 throw new ArgumentException();
            
}

             return result.ToString();
        }

        static void Main(string[] args)
        {

Console.WriteLine(DateConvert.Instance.Baodate2Chinese("2007-11-4"));
           
Console.WriteLine(DateConvert.Instance.Baodate2Chinese("07-11-4"));
           
Console.WriteLine(DateConvert.Instance.Baodate2Chinese("2007/11/4"));
           
Console.WriteLine(DateConvert.Instance.Baodate2Chinese("07/11/4"));
       
}
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.