Digital date to Chinese date in C #

Source: Internet
Author: User
Tags split tostring

Convert the digital date "2007-11-4" to the Chinese date "November 4, 2007".

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[] {


' 0 ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six ', ' seven ', ' eight ', ' nine ', ' Ten '


       };


     }


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 ();


//Based on regular expressions to determine whether the parameters are correct


regex thereg = new Regex (D{2}|d{4}) (/|-) (d{1,2}) (/|-) (d {1,2}) ");


if (Thereg.match (strdate). Length!= 0)


       {


//Saves the date of a number to the character array str


string[] str = NULL;


if (Strdate.contains ("-"))


         {


str = strdate.split ('-');


         }


Else if (strdate.contains ("/"))


         {


str = strdate.split ('/');


         }


//str[0] in the year, converts its individual characters to the corresponding kanji


for (int i = 0; i < str[0]. Length; i++)


         {


Result. Append (Strchinese[int. Parse (Str[0][i]. ToString ())]);


         }


result. Append ("year");


//Convert month


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 ("month");


//Convert Day


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 ("Day");


       }


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"));


    }


  }


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.