【问题标题】:How can a Timespan-Day last only for 8 Hours?时间跨度日怎么可能只持续 8 小时?
【发布时间】:2010-08-16 12:11:30
【问题描述】:

我以分钟为单位保存了一个持续时间,并希望输出“1 天 5 小时 30 分钟”。目前我将分钟添加到时间跨度并执行以下操作:

TimeSpan ts = new TimeSpan(0,0,1800, 0);
Response.Write(ts.Days + "day(s) " + ts.Hours + " hour(s)" + ts.Minutes + " minutes");

但现在我只对一天的工作时间感兴趣。因此,当 TimeSpan 的持续时间为 27 小时时,它不应创建输出“1 天 3 小时”。我想要“3天3小时”。

是否有一种简单的方法可以使用 Timespan 对象来做到这一点?是否可以更改 TimeSpan 的默认行为?还是我必须编写自己的自定义时间跨度类?

谢谢 cpt.oneeye

【问题讨论】:

  • 这里有一个提示:div 和 mod。
  • @theburningmonk:是的...12 小时会更现实,因为我们用它来跟踪我们自己的工作时间:)

标签: c# timespan


【解决方案1】:

你能简单地使用吗:

(int)(ts.TotalHours / 8)

而不是ts.Days?然后使用

(((int)ts.TotalHours) % 8)

而不是ts.Hours。

【讨论】:

    【解决方案2】:

    你需要实现这样的东西:

    TimeSpan workday = new TimeSpan(8, 0, 0);
    int workdays = ts.Ticks / workday.Ticks
    TimeSpan rest = new TimeSpan(ts.Ticks % workday.Ticks)
    Response.Write(workdays  + "workday(s) and" + rest.ToString());
    

    会写一些类似的东西

    "3 workday(s) and 3:32"
    

    【讨论】:

      【解决方案3】:

      这是我的代码,供需要的人使用。

      DynamicTimeSpan 类:

      using System;
      using System.Text;
      
      namespace fooLib
      {
          /// <summary>
          /// Timespan where you can define the number of hours a day can last (+ days of week).
          /// Optimal for calculating bussinesshours.
          /// </summary>
          public class DynamicTimeSpan
          {
              private int _hoursPerDay = 8;
              private int _daysPerWeek = 5;
              private int _totalMinutes = 0;
      
              public int HoursPerDay
              {
                  get { return _hoursPerDay; }
                  set { _hoursPerDay = value; }
              }
      
              public int DaysPerWeek
              {
                  get { return _daysPerWeek; }
                  set { _daysPerWeek = value; }
              }
      
              public int Weeks
              {
                  get
                  {
                      return  (int)(((_totalMinutes / 60) / this.HoursPerDay) / this.DaysPerWeek);
                  }
              }
      
              public int Days
              {
                  get
                  {
                      return (int)((decimal)TotalDays - (decimal)(Weeks * this.DaysPerWeek));
                  }
              }
      
              public int Hours
              {
                  get
                  {
                      return (int)((decimal)TotalHours - (decimal)(Weeks * this.DaysPerWeek * this.HoursPerDay) - (decimal)(Days * this.HoursPerDay));
                  }
              }
      
              public int Minutes
              {
                  get
                  {
                      return _totalMinutes - (Weeks * this.DaysPerWeek * this.HoursPerDay * 60) - (Days * this.HoursPerDay * 60) - (Hours * 60);
                  }
              }
      
              public decimal TotalDays
              {
                  get { return (decimal)_totalMinutes / (decimal)60 / (decimal)this.HoursPerDay; }
              }
      
              public decimal TotalHours
              {
                  get { return (decimal)_totalMinutes / (decimal)60; }
              }
      
              public int TotalMinutes
              {
                  get { return _totalMinutes; }
              }
      
              public static DynamicTimeSpan operator +(DynamicTimeSpan ts1, DynamicTimeSpan ts2)
              {
                  return new DynamicTimeSpan(ts1._totalMinutes + ts2._totalMinutes);
              }
      
              public static DynamicTimeSpan operator -(DynamicTimeSpan ts1, DynamicTimeSpan ts2)
              {
                  return new DynamicTimeSpan(ts1._totalMinutes - ts2._totalMinutes);
              }
      
              public DynamicTimeSpan()
              {
      
              }
      
              public DynamicTimeSpan(int totalMinutes)
              {
                  _totalMinutes = totalMinutes;
              }
      
              public DynamicTimeSpan(int weeks, int days, int hours, int minutes)
              {
                  _totalMinutes = (weeks * this.DaysPerWeek * this.HoursPerDay * 60) + (days * this.HoursPerDay * 60) + (hours * 60) + minutes;
              }
      
              /// <summary>
              /// "1 week 2 days 4 hours 30 minutes"
              /// </summary>
              /// <returns></returns>
              public override string ToString()
              {
                  string str = "";
      
                  if (this.Weeks == 1)
                  {
                      str += this.Weeks + " week ";
                  }
                  else if (this.Weeks > 1)
                  {
                      str += this.Weeks + " weeks ";
                  }
      
                  if (this.Days == 1)
                  {
                      str += this.Days + " day ";
                  }
                  else if (this.Days > 1)
                  {
                      str += this.Days + " days ";
                  }
      
                  if (this.Hours == 1)
                  {
                      str += this.Hours + " hour ";
                  }
                  else if (this.Hours > 1)
                  {
                      str += this.Hours + " hours ";
                  }
      
                  // only write minutes when the duration is lower than one day
                  if (this.Weeks == 0 && this.Days == 0 && this.Minutes > 0)
                  {
                      str += this.Minutes + " minutes";
                  }
      
                  return str;
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-20
        • 1970-01-01
        • 1970-01-01
        • 2015-10-23
        • 1970-01-01
        • 2018-05-06
        • 2023-01-20
        • 1970-01-01
        相关资源
        最近更新 更多