zoukankan      html  css  js  c++  java
  • C# 根据给定 小时 分钟 计算给定时间段的偏差时长

     private void button5_Click(object sender, EventArgs e)
            {
                string str = "8.00~12.30";
                int index = str.IndexOf('~');
                int length = str.Length - index - 1;
                string begin = str.Substring(0, index);
                string end = str.Substring(index + 1, length);
    
                string timea = "8:30";
                string timeb = "11:30";
                DateTime beginTime = Convert.ToDateTime(timea);
                DateTime endTime = Convert.ToDateTime(timeb);
    
                var aaa = GetTimeLength(beginTime, endTime);
                var bbb = GetTimeLenght2(beginTime, endTime);
                MessageBox.Show("GetTimeLength==" + aaa + "
    " + bbb);
            }
    
            public string GetTimeLength(DateTime beginTime, DateTime endTime)
            {
                try
                {
                    string timeLength = string.Empty;
                    TimeSpan begin = new TimeSpan(beginTime.Ticks);
                    TimeSpan end = new TimeSpan(endTime.Ticks);
                    TimeSpan ts = begin.Subtract(end).Duration();
    
                    timeLength = Convert.ToString(ts.Hours * 60 + ts.Minutes);
    
                    return timeLength;
                }
                catch
                {
                }
                return string.Empty;
            }
    
    
            public string GetTimeLenght2(DateTime DateTime1, DateTime DateTime2)
            {
                string dateDiff = string.Empty;
                try
                {
                    TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
                    TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
                    TimeSpan ts = ts1.Subtract(ts2).Duration();
                    string hours = ts.Hours.ToString(), minutes = ts.Minutes.ToString(), seconds = ts.Seconds.ToString();
                    if (ts.Hours < 10)
                    {
                        hours = "0" + ts.Hours.ToString();
                    }
                    if (ts.Minutes < 10)
                    {
                        minutes = "0" + ts.Minutes.ToString();
                    }
                    if (ts.Seconds < 10)
                    {
                        seconds = "0" + ts.Seconds.ToString();
                    }
                    dateDiff = hours + ":" + minutes + ":" + seconds;
                }
                catch
                {
                }
                return dateDiff;
            }
    

     

  • 相关阅读:
    npx 是什么?
    JavaScript 的内置对象和浏览器对象
    JS构造函数new的过程
    git 设置和取消代理
    npm配置镜像、设置代理
    SQL 注入攻击案例
    javascript:void(0);的含义以及使用场景
    让所有网页图片跳起舞来的代码
    针对Web的攻击技术
    网站常见的鉴权认证方式有哪几种?
  • 原文地址:https://www.cnblogs.com/YYkun/p/12794201.html
Copyright © 2011-2022 走看看