C#中将数字日期转换为中文日期

        下面的代码可以将数字日期如:2005-12-30转换为“二〇〇五年十二月三十日”
代码如下:
  1 None.gif using  System;
  2 None.gif using  System.Text;
  3 None.gif using  System.Text.RegularExpressions;
  4 None.gif
  5 None.gif namespace  CSharpDateConvert
  6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  7InBlock.gif    class DateConvert
  8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
  9InBlock.gif        private static DateConvert m_DateConvert = null;
 10InBlock.gif
 11InBlock.gif        private char[] strChinese;
 12InBlock.gif
 13InBlock.gif        private DateConvert()
 14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 15ExpandedSubBlockStart.gifContractedSubBlock.gif            strChinese = new char[] dot.gif{
 16InBlock.gif                '','','','','','','','','','',''
 17ExpandedSubBlockEnd.gif            }
;
 18ExpandedSubBlockEnd.gif        }

 19InBlock.gif
 20InBlock.gif        public static DateConvert Instance
 21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 22InBlock.gif            get
 23ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 24InBlock.gif                if (m_DateConvert == null)
 25InBlock.gif                    m_DateConvert = new DateConvert();
 26InBlock.gif                return m_DateConvert;
 27ExpandedSubBlockEnd.gif            }

 28ExpandedSubBlockEnd.gif        }

 29InBlock.gif
 30InBlock.gif        public string Baodate2Chinese(string strDate)
 31ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 32InBlock.gif            StringBuilder result = new StringBuilder();
 33InBlock.gif
 34InBlock.gif            // 依据正则表达式判断参数是否正确
 35InBlock.gif            Regex theReg = new Regex(@"(\d{2}|\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})");
 36InBlock.gif            if (theReg.Match(strDate).Length != 0)
 37ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 38InBlock.gif                // 将数字日期的年月日存到字符数组str中
 39InBlock.gif                string[] str = null;
 40InBlock.gif                if (strDate.Contains("-"))
 41ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 42InBlock.gif                    str = strDate.Split('-');
 43ExpandedSubBlockEnd.gif                }

 44InBlock.gif                else if (strDate.Contains("/"))
 45ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 46InBlock.gif                    str = strDate.Split('/');
 47ExpandedSubBlockEnd.gif                }

 48InBlock.gif
 49InBlock.gif                // str[0]中为年,将其各个字符转换为相应的汉字
 50InBlock.gif                for (int i = 0; i < str[0].Length; i++)
 51ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 52InBlock.gif                    result.Append(strChinese[int.Parse(str[0][i].ToString())]);
 53ExpandedSubBlockEnd.gif                }

 54InBlock.gif                result.Append("");
 55InBlock.gif
 56InBlock.gif                // 转换月
 57InBlock.gif                int month = int.Parse(str[1]);
 58InBlock.gif                int MN1 = month / 10;
 59InBlock.gif                int MN2 = month % 10;
 60InBlock.gif
 61InBlock.gif                if (MN1 > 1)
 62ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 63InBlock.gif                    result.Append(strChinese[MN1]);
 64ExpandedSubBlockEnd.gif                }

 65InBlock.gif                if (MN1 > 0)
 66ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 67InBlock.gif                    result.Append(strChinese[10]);
 68ExpandedSubBlockEnd.gif                }

 69InBlock.gif                if (MN2 != 0)
 70ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 71InBlock.gif                    result.Append(strChinese[MN2]);
 72ExpandedSubBlockEnd.gif                }

 73InBlock.gif                result.Append("");
 74InBlock.gif
 75InBlock.gif                // 转换日
 76InBlock.gif                int day = int.Parse(str[2]);
 77InBlock.gif                int DN1 = day / 10;
 78InBlock.gif                int DN2 = day % 10;
 79InBlock.gif
 80InBlock.gif                if (DN1 > 1)
 81ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 82InBlock.gif                    result.Append(strChinese[DN1]);
 83ExpandedSubBlockEnd.gif                }

 84InBlock.gif                if (DN1 > 0)
 85ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 86InBlock.gif                    result.Append(strChinese[10]);
 87ExpandedSubBlockEnd.gif                }

 88InBlock.gif                if (DN2 != 0)
 89ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 90InBlock.gif                    result.Append(strChinese[DN2]);
 91ExpandedSubBlockEnd.gif                }

 92InBlock.gif                result.Append("");
 93ExpandedSubBlockEnd.gif            }

 94InBlock.gif            else
 95ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 96InBlock.gif                throw new ArgumentException();
 97ExpandedSubBlockEnd.gif            }

 98InBlock.gif
 99InBlock.gif            return result.ToString();
100ExpandedSubBlockEnd.gif        }

101InBlock.gif
102InBlock.gif        static void Main(string[] args)
103ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
104InBlock.gif            Console.WriteLine(DateConvert.Instance.Baodate2Chinese("2005-10-10"));
105InBlock.gif            Console.WriteLine(DateConvert.Instance.Baodate2Chinese("05-12-31"));
106InBlock.gif            Console.WriteLine(DateConvert.Instance.Baodate2Chinese("2005/10/10"));
107InBlock.gif            Console.WriteLine(DateConvert.Instance.Baodate2Chinese("05/12/31"));
108ExpandedSubBlockEnd.gif        }

109ExpandedSubBlockEnd.gif    }

110ExpandedBlockEnd.gif}

111 None.gif

转载于:https://www.cnblogs.com/srw962/archive/2005/08/19/218301.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值