zoukankan      html  css  js  c++  java
  • 日期的格式化显示


    我们的日期以前显示为:2010年10月15日 2010年10月16日 2011年10月18日 2011年10月17日 

    需要修改为: 2010年10月15日 - 17日;

    规则是:如果连续中间 用-隔开,如果不连续用、隔开。

    代码如下: 

    public static string FormatDepartDate(string AllDepartDate)
            {
                
    //2010年10月15日
                
                
    string[] Array_AllDepartDate = AllDepartDate.Split(' ');            

                IEnumerable
    <String> querys = Array_AllDepartDate.OrderBy(a => Convert.ToDateTime(a));

                StringBuilder m_FormatDate 
    = new StringBuilder(512);

                
    for (int iLoop = 0; iLoop < querys.Count(); iLoop++)
                {
                    
    if (iLoop == 0)
                    {
                        m_FormatDate.Append(querys.ElementAt(
    0).ToString());
                    }
                    
    else
                    {
                        DateTime m_DateTime 
    = Convert.ToDateTime(querys.ElementAt(iLoop - 1));
                        DateTime m_DateTime1 
    = Convert.ToDateTime(querys.ElementAt(iLoop));

                        
    //判断是否同一年
                        if (m_DateTime1.Year == m_DateTime.Year)
                        {
                            
    //是同一个月
                            if (m_DateTime1.Month == m_DateTime.Month)
                            {
                                
    //判断日期是否连续
                                IsEqualDay(m_FormatDate, ref m_DateTime, ref m_DateTime1);
                            }
                            
    else
                            {
                                m_FormatDate.AppendFormat(
    "、{0}月{1}日", m_DateTime1.Month.ToString("00"), m_DateTime1.Day.ToString("00"));
                            }
                        }
                        
    else
                        {
                            m_FormatDate.AppendFormat(
    "、{0}年{1}月{2}日", m_DateTime1.Year.ToString("0000"), m_DateTime1.Month.ToString("00"), m_DateTime1.Day.ToString("00"));
                        }
                    }
                }

                
    return m_FormatDate.ToString();
            }

            
    private static void IsEquelDay(StringBuilder m_FormatDate, ref DateTime m_DateTime, ref DateTime m_DateTime1)
            {
                
    if (m_DateTime1.Day - m_DateTime.Day == 1)
                {
                    
    if (m_FormatDate.ToString().LastIndexOf(string.Format("-{0}", m_DateTime.Day.ToString("00"))) > 0//去掉连续的日期 用-代替。
                        m_FormatDate.Replace(string.Format("-{0}", m_DateTime.Day.ToString("00")), string.Format("-{0}", m_DateTime1.Day.ToString("00")));
                    
    else
                        m_FormatDate.AppendFormat(
    "-{0}日", m_DateTime1.Day.ToString("00"));
                }
                
    else
                {
                    m_FormatDate.AppendFormat(
    "、{0}日", m_DateTime1.Day.ToString("00"));
                }
            }

    调用如下:

    string a = "2010年10月15日 2010年10月16日 2011年10月15日 2011年10月17日";
                            
    Response.Write(FormatDepartDate(a));

    这个方法我不是很满意,也不知道怎么优化了,如果大家有好的方法,请分享出来。 谢谢了!

     缺点: 

    1.判断日期连续的地方逻辑复杂。 

    2.循环的日期转换,比较耗费性能。

    作者:管宇

    欢迎转载,请标明出处。

  • 相关阅读:
    django 登录验证的方法
    css table细边框样式
    django ORM创建实体,指定表,指定字段
    Kafka学习之路 (一)Kafka的简介
    NPIO导入导出excel文件
    .net里面<app.config>中value值不能填写特殊符号问题
    创建服务
    读取文本内容
    vs code 代码片段、提示
    .net core worker service安装运行再windows或linux
  • 原文地址:https://www.cnblogs.com/winner2009/p/1851872.html
Copyright © 2011-2022 走看看