当前位置:编程学习 > C#/ASP.NET >>

将日期时间转换为指定格式

        #region  将日期转换成指定的格式
        /// <summary>
        /// 将日期转换成yyyy-mm-dd格式.
        /// </summary>
        /// <param name="NDate">日期</param>
        /// <returns>返回String对象</returns>
        public string Date_Format(string NDate)
        {
            string sm, sd;
            int y, m, d;
            try
            {
                y = Convert.ToDateTime(NDate).Year;
                m = Convert.ToDateTime(NDate).Month;
                d = Convert.ToDateTime(NDate).Day;
            }
            catch
            {
                return "";
            }
            if (y == 1900)
                return "";
            if (m < 10)
                sm = "0" + Convert.ToString(m);
            else
                sm = Convert.ToString(m);
            if (d < 10)
                sd = "0" + Convert.ToString(d);
            else
                sd = Convert.ToString(d);
            return Convert.ToString(y)  + sm  + sd;
        }
        #endregion

        #region  将日期转换成指定的格式
        /// <summary>
        /// 将日期转换成yyyy-mm-dd格式.
        /// </summary>
        /// <param name="NDate">日期</param>
        /// <returns>返回String对象</returns>
        public string Date_Format_(string NDate)
        {
            string sm, sd;
            int y, m, d;
            try
            {
                y = Convert.ToDateTime(NDate).Year;
                m = Convert.ToDateTime(NDate).Month;
                d = Convert.ToDateTime(NDate).Day;
            }
            catch
            {
                return "";
            }
            if (y == 1900)
                return "";
            if (m < 10)
                sm = "0" + Convert.ToString(m);
            else
                sm = Convert.ToString(m);
            if (d < 10)
                sd = "0" + Convert.ToString(d);
            else
                sd = Convert.ToString(d);
            return Convert.ToString(y) + "-" + sm + "-" + sd;
        }
        #endregion


        #region  将时间转换成指定的格式
        /// <summary>
        /// 将时间转换成hhmmss格式.
        /// </summary>
        /// <param name="NDate">日期</param>
        /// <returns>返回String对象</returns>
        public string Time_Format(string NDate)
        {
            string sh, sm, se;
            int hh, mm, ss;
            try
            {
                hh = Convert.ToDateTime(NDate).Hour;
                mm = Convert.ToDateTime(NDate).Minute;
                ss = Convert.ToDateTime(NDate).Second;

            }
            catch
            {
                return "";
            }
            sh = Convert.ToString(hh);
            if (sh.Length < 2)
                sh = "0" + sh;
            sm = Convert.ToString(mm);
            if (sm.Length < 2)
                sm = "0" + sm;
            se = Convert.ToString(ss);
            if (se.Length < 2)
                se = "0" + se;
            return sh + sm + se;
        }


        #endregion

        #region 返回excel的路径名
        
        internal string getExcelFileName()//打开excel文件
        {
            string name = "";
            try
            {
                OpenFileDialog openCsv = new OpenFileDialog();
                openCsv.Filter = "Excel 文件|*.xls;*.csv;*.et";
                if (openCsv.ShowDialog() == DialogResult.OK)
                {
                    name = openCsv.FileName;
                    return name;
                }
            }
            catch (OleDbException ode)
            {
                MessageBox.Show(ode.ToString());
                return null;
            }
            catch (Exception exc)
            {
                MessageBox.Show("未打开文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Console.WriteLine(exc.ToString());

            }
            return null;

        } //打开文件

        #endregion

        #region 返回时间格式到  yyyymmddhhmmss  20130930074600
        
        internal string changeDateTimeFormat(string cardTime)
        {
            string dateFormat = Date_Format(cardTime);
            string timeFormat = Time_Format(cardTime);

            string dateAndTime = dateFormat + timeFormat;
            return dateAndTime;
        }

        #endregion

 

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,