C#中的一种简单的日期格式转换

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            
            Console.WriteLine("请输入日期:XXXX年XX月XX日");
            //调用日期转换函数
            string convertDate=ToCHDate(Console.ReadLine());
            //输出转换格式后的日期
            Console.WriteLine("转换后的日期格式为:"+convertDate);
        }
        //日期转换函数
        static string ToCHDate(string date)
        {
            char[] chars = date.ToCharArray();
            for (int i = 0; i < date.Length; i++)
            {
                char ch =chars[i];
                //使用switch-case
                switch(ch)
                {
                    default:
                        break;
                    case '0':
                        chars[i]='';
                        break;
                    case '1':
                        chars[i] = '';
                        break;
                    case '2':
                        chars[i] = '';
                        break;
                    case '3':
                        chars[i] = '';
                        break;
                    case '4':
                        chars[i] = '';
                        break;
                    case '5':
                        chars[i] = '';
                        break;
                    case '6':
                        chars[i] = '';
                        break;
                    case '7':
                        chars[i] = '';
                        break;
                    case '8':
                        chars[i] = '';
                        break;
                    case '9':
                        chars[i] = '';
                        break;
                }
            }
            //关键语句date.TocharArray()配合默契
            string date1 = new string(chars);
            return date1;
        }
    }
}

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;
namespace ConsoleApplication2{    class Program    {        static void Main(string[] args)        {                        Console.WriteLine("请输入日期:XXXX年XX月XX日");            //调用日期转换函数            string convertDate=ToCHDate(Console.ReadLine());            //输出转换格式后的日期            Console.WriteLine("转换后的日期格式为:"+convertDate);        }        //日期转换函数        static string ToCHDate(string date)        {            char[] chars = date.ToCharArray();            for (int i = 0; i < date.Length; i++)            {                char ch =chars[i];                //使用switch-case                switch(ch)                {                    default:                        break;                    case '0':                        chars[i]='零';                        break;                    case '1':                        chars[i] = '一';                        break;                    case '2':                        chars[i] = '二';                        break;                    case '3':                        chars[i] = '三';                        break;                    case '4':                        chars[i] = '四';                        break;                    case '5':                        chars[i] = '五';                        break;                    case '6':                        chars[i] = '六';                        break;                    case '7':                        chars[i] = '七';                        break;                    case '8':                        chars[i] = '八';                        break;                    case '9':                        chars[i] = '九';                        break;                }            }            //关键语句date.TocharArray()配合默契            string date1 = new string(chars);            return date1;        }    }}

 

posted @ 2017-02-16 15:44  一直为你守候  阅读(441)  评论(0编辑  收藏  举报