zoukankan      html  css  js  c++  java
  • 格式化日期字符串

    第一种是我以前常用的,但比较麻烦(后面还有更简洁的):

        /// <summary>
        /// 格式化日期字符串
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        private string DateDiff(string dt, int type)
        {
            string time = dt;
            try
            {
                DateTime t = Convert.ToDateTime(dt);
                string year = t.Year.ToString();
                string month = t.Month.ToString("00");
                string day = t.Day.ToString("00");
                string hour = t.Hour.ToString("00");
                string minute = t.Minute.ToString("00");
    
                switch (type)
                {
                    case 1:
                        time = string.Format("{0}-{1}-{2} {3}:{4}", year, month, day, hour, minute);
                        break;
                    case 2:
                        time = string.Format("{0}-{1}-{2}", year, month, day);
                        break;
                    default:
                        time = string.Format("{0}-{1}-{2}", year, month, day);
                        break;
                }
            }
            catch (Exception)
            {
    
            }
            return time;
        }

    上面这种比较麻烦,下面有种更简洁的办法:

        /// <summary>
        /// 返回日期格式化字符串
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        private string Datediff(string dt, int type)
        {
            string t = dt;
            try
            {
                DateTime date = Convert.ToDateTime(dt);
                switch (type)
                {
                    case 1:
                        t = date.ToString("yyyy-MM-dd HH:mm");
                        break;
                    case 2:
                        t = date.ToString("yyyy-MM-dd");
                        break;
                    default:
                        t = date.ToString("yyyy-MM-dd");
                        break;
                }
            }
            catch (Exception)
            {
    
            }
            return t;
        }

    调用的办法很简单,如下:

            string sql = "SELECT * FROM HR_ProductBOMList_V " + condition + " ORDER BY [FormNo] DESC";
    
            gvExcel.DataSource = DbHelperSQL.Query(sql);
    
            gvExcel.DataBind();
    
            int nCount = gvExcel.Rows.Count;
            string createTime,dt;
            for (int i = 0; i < nCount; i++)
            {
                dt = gvExcel.Rows[i].Cells[9].Text;
    
                gvExcel.Rows[i].Cells[9].Text = Datediff(dt, 1);
            }
  • 相关阅读:
    【LeetCode OJ】Remove Element
    【LeetCode OJ】Remove Duplicates from Sorted Array
    【LeetCode OJ】Swap Nodes in Pairs
    【LeetCode OJ】Merge Two Sorted Lists
    【LeetCode OJ】Remove Nth Node From End of List
    【LeetCode OJ】Two Sum
    【LeetCode OJ】Majority Element
    最长公共子序列问题
    php fopen与file_get_contents的区别
    PHP 技巧集合
  • 原文地址:https://www.cnblogs.com/seasons1987/p/3066299.html
Copyright © 2011-2022 走看看