• C# 计算时间间隔,两个时间差(年月日时分秒)


     参考:https://www.cnblogs.com/hllive/articles/10592812.html

        public class Program
        {
            /// <summary>
            /// 计算两个时间差(年月日时分秒)
            /// </summary>
            /// <param name="b">开始时间</param>
            /// <param name="e">结束时间</param>
            /// <returns></returns>
            private static string term(DateTime b, DateTime e)
            {
                if (b < e)
                {
                    var t = new
                    {
                        bm = b.Month,
                        em = e.Month,
                        bd = b.Day,
                        ed = e.Day
                    };
                    int diffMonth = (e.Year - b.Year) * 12 + (t.em - t.bm),//相差月
                        diffYear = diffMonth / 12;//相差年
    
                    int[] d = new int[3] { 0, 0, 0 };
                    if (diffYear > 0)
                    {
                        if (t.em == t.bm && t.ed < t.bd)
                        {
                            d[0] = diffYear - 1;
                        }
                        else d[0] = diffYear;
                    }
    
                    if (t.ed >= t.bd)
                    {
                        d[1] = diffMonth % 12;
                        d[2] = t.ed - t.bd;
                    }
                    else//结束日 小于 开始日
                    {
                        int dm = diffMonth - 1;
                        d[1] = dm % 12;
                        TimeSpan ts = e - b.AddMonths(dm);
                        d[2] = ts.Days;
                    }
                    StringBuilder sb = new StringBuilder();
    
                    if (d.Sum() > 0)
                    {
                        if (d[0] > 0) sb.Append(string.Format("{0}年", d[0]));
                        if (d[1] > 0) sb.Append(string.Format("{0}个月", d[1]));
                        if (d[2] > 0) sb.Append(string.Format("{0}天", d[2]));
                    }
                    else
                    {
                        int[] time = new int[2] { 0, 0 };
                        TimeSpan sj = e - b;
                        time[0] = sj.Hours;
                        time[1] = sj.Minutes % 60;
                        if (time[0] > 0) sb.Append(string.Format("{0}小时", time[0]));
                        if (time[1] > 0) sb.Append(string.Format("{0}分钟", time[1]));
                        if (time.Sum() <= 0) sb.Append(string.Format("{0}秒", sj.Seconds));
                    }
                    return sb.ToString();
                }
                else throw new Exception("开始日期必须小于结束日期");
            }
            static void Main(string[] args)
            {
                DateTime begin = new DateTime(2021, 6, 20,16,47,9);
                DateTime end = DateTime.Now;
                string s = term(begin, end);
                Console.WriteLine(s);
                //匹配汉字 [u4e00-u9fa5]
                string[] sl = System.Text.RegularExpressions.Regex.Split(s, "[u4e00-u9fa5]+", RegexOptions.None);
                Console.WriteLine(sl);
                string[] sl2 = System.Text.RegularExpressions.Regex.Split(s, "[0-9]+", RegexOptions.None);
                Console.WriteLine(sl2);
                string[] sl3 = System.Text.RegularExpressions.Regex.Split(s, "([0-9]+)");
                Console.WriteLine(sl3);
            }
        }

    ****

    常记溪亭日暮,沉醉不知归路。兴尽晚回舟,误入藕花深处。争渡,争渡,惊起一滩鸥鹭。

    昨夜雨疏风骤,浓睡不消残酒。试问卷帘人,却道海棠依旧。知否?知否?应是绿肥红瘦。
  • 相关阅读:
    4步搞定:系统必备的安装位置未设置为组件供应商的网站,无法在磁盘上找到 dotNetFx40LP_Client_x86_x64cs.exe 问题的解决方案 VS2010 SP1 简体中文测试通过,繁体未测试
    SilverLight Expression 140小时视频讲座
    获取本地IP铁通也可以
    USB转485转换器
    继电器控制板
    远程抄表解决方案汇总
    Silverlight 鼠标滚轮组件“Silverlight.FX”
    Silverlight边学边写之一“Silverlight+Webservice+Dataset”综合应用
    企业库缓存类(EnterpriseLibrary CacheHelper )
    Silverlight Toolkit 正式版今天发布啦!
  • 原文地址:https://www.cnblogs.com/htj10/p/15324947.html
Copyright © 2020-2023  润新知