其他分享
首页 > 其他分享> > 获取规定法则的年月日

获取规定法则的年月日

作者:互联网

根据传入的日期,年份(整数),循环数(整数),及字符串数组等参数,返回三位数的“年月日"字符串;

比如,定义字符串数组string[] a=new string[31] 

{ "1","2","3","4","5","6","7","8","9",
  "A","B","C","D","E","F","G","H","J",
  "K","L","M","N","P","R","S","T","V",
  "W","X","Y","O"};

然后执行方法getYMD(DateTime.Prase("2021-10-22",2011,30,a),则返回"BBN"。

 

        /// <summary>
        /// 获取规定法则的年月日
        /// </summary>
        /// <param name="date">日期</param>
        /// <param name="start">开始计算的年份整数</param>
        /// <param name="recycle">轮回数(即多少年循环一次)</param>
        /// <param name="a">年份对应的编码数组</param>
        /// <returns></returns>
        public string getYMD(DateTime date,int start,int recycle,string[] a)
        {
            //string[] a = new string[31] { "1","2","3","4","5","6","7","8","9",
            //                              "A","B","C","D","E","F","G","H","J",
            //                              "K","L","M","N","P","R","S","T","V",
            //                              "W","X","Y","O"};
            string yyyyMMdd = date.ToString("yyyyMMdd");    //把日期转换为八位数的年月日字符串
            int y = int.Parse(yyyyMMdd.Substring(0, 4));    //取年份整数
            int m = int.Parse(yyyyMMdd.Substring(4, 2));    //取月份整数
            int d = int.Parse(yyyyMMdd.Substring(6));       //取日期整数

            string strY = a[(y-start) % recycle ];
            string strM = a[m - 1];
            string strD = a[d - 1];
            yyyyMMdd = strY + strM + strD;
            return yyyyMMdd;
        }

 

标签:法则,string,int,整数,Substring,获取,yyyyMMdd,年月日
来源: https://www.cnblogs.com/lxh168/p/15388831.html