C# 将数字格式转换为日期格式

        public static string ChgNumToDate(int Numeric, string strDateFormat)
        {
            string NumToDate = "";
            string strNumeric = Convert.ToString(Numeric);
            strDateFormat = strDateFormat.ToUpper();
            int intFmtCount = 0;
            string strTmpChar;
            for (int intCount = 0; intCount <= strDateFormat.Length - 1; intCount++)
            {
                string strFmtChar = strDateFormat.Substring(intCount, 1);
                if (strFmtChar == "Y" || strFmtChar == "M" || strFmtChar == "D")
                {
                    if (intFmtCount <= strNumeric.Length)
                    {
                        strTmpChar = strNumeric.Substring(intFmtCount, 1);
                        intFmtCount = intFmtCount + 1;

                        if (strTmpChar != "")
                        {
                            NumToDate = NumToDate + strTmpChar;
                        }
                    }
                    else
                    {
                        NumToDate = NumToDate + strFmtChar;
                    }
                }
                else
                {
                    NumToDate = NumToDate + strFmtChar;
                }
            }

            return NumToDate;
        }

        public static string ChgNumToTime(int Numeric, string strTimeFormat)
        {
            string NumToDate = "";
            string strNumeric = Convert.ToString(Numeric);
            //do
            //{
            //    strNumeric = "0" + strNumeric;

            //} while (strNumeric.Length < 6);

            while (strNumeric.Length < 6)
            {
                strNumeric = "0" + strNumeric;
            }

            strTimeFormat = strTimeFormat.ToUpper();
            int intFmtCount = 0;
            string strTmpChar;
            for (int intCount = 0; intCount <= strTimeFormat.Length - 1; intCount++)
            {
                string strFmtChar = strTimeFormat.Substring(intCount, 1);
                if (strFmtChar == "H" || strFmtChar == "M" || strFmtChar == "S")
                {
                    if (intFmtCount <= strNumeric.Length)
                    {
                        strTmpChar = strNumeric.Substring(intFmtCount, 1);
                        intFmtCount = intFmtCount + 1;

                        if (strTmpChar != "")
                        {
                            NumToDate = NumToDate + strTmpChar;
                        }
                    }
                    else
                    {
                        NumToDate = NumToDate + strFmtChar;
                    }
                }
                else
                {
                    NumToDate = NumToDate + strFmtChar;
                }
            }

            return NumToDate;
        }

 

posted @ 2023-07-12 14:59  它的眼角开过光  阅读(859)  评论(0编辑  收藏  举报