Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 一天的时间跨度怎么可能只有8个小时?_C#_Timespan - Fatal编程技术网

C# 一天的时间跨度怎么可能只有8个小时?

C# 一天的时间跨度怎么可能只有8个小时?,c#,timespan,C#,Timespan,我已将持续时间保存为分钟,并希望有一个输出“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 workday = new TimeSpan(8, 0, 0); int workdays = ts.Tic

我已将持续时间保存为分钟,并希望有一个输出“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 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());
但现在我只对一天的工作时间感兴趣。因此,当时间跨度的持续时间为27小时时,不应创建输出“1天3小时”。我想要“三天三小时”

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

Thx cpt.oneeye

您可以简单地使用:

(int)(ts.TotalHours / 8)
而不是
ts.Days
?然后使用

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

您需要实现如下内容,而不是
ts.Hours

TimeSpan ts = new TimeSpan(0,0,1800, 0);
Response.Write(ts.Days + "day(s) " + ts.Hours + " hour(s)" + ts.Minutes + " minutes");
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"

这是我给那些需要它的人的代码

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;
        }
    }
}
使用系统;
使用系统文本;
名称空间傻瓜
{
/// 
///Timespan,您可以在其中定义一天可以持续的小时数(+一周的天数)。
///最适合计算营业时间。
/// 
公共类动态mespan
{
私人国际小时/天=8;
私人国际日周=5;
私有整数_totalMinutes=0;
每日公共小时
{
获取{return\u hoursPerDay;}
设置{u hoursPerDay=value;}
}
公共国际日/周
{
获取{return\u daysPerWeek;}
设置{u daysPerWeek=value;}
}
公共整数周
{
得到
{
返回(int)((((totalMinutes/60)/this.HoursPerDay)/this.DaysPerWeek);
}
}
公众整数日
{
得到
{
返回值(整数)((十进制)总天数-(十进制)(周*本天/周));
}
}
公共整数小时
{
得到
{
return(int)((十进制)TotalHours-(十进制)(周*this.DaysPerWeek*this.HoursPerDay)-(十进制)(天*this.HoursPerDay));
}
}
公共整数分钟
{
得到
{
返回_totalMinutes-(Weeks*this.DaysPerWeek*this.HoursPerDay*60)-(Days*this.HoursPerDay*60)-(Hours*60);
}
}
公共十进制天数
{
获取{return(decimal)_totalMinutes/(decimal)60/(decimal)this.HoursPerDay;}
}
公共十进制总小时数
{
获取{return(decimal)_totalMinutes/(decimal)60;}
}
公共整数分钟
{
获取{return\u totalMinutes;}
}
公共静态DynamicMespan运算符+(DynamicMespan ts1、DynamicMespan ts2)
{
返回新的DynamicTimeSpan(ts1.\u totalMinutes+ts2.\u totalMinutes);
}
公共静态dynamicmespan运算符-(dynamicmespan ts1,dynamicmespan ts2)
{
返回新的DynamicTimeSpan(ts1.\u totalMinutes-ts2.\u totalMinutes);
}
公共动态
{
}
公共动态时间跨度(整数分钟)
{
_totalMinutes=totalMinutes;
}
公共动态时间跨度(整数周、整数天、整数小时、整数分钟)
{
_totalMinutes=(周*this.DaysPerWeek*this.HoursPerDay*60)+(天*this.HoursPerDay*60)+(小时*60)+分钟;
}
/// 
///“1周2天4小时30分钟”
/// 
/// 
公共重写字符串ToString()
{
字符串str=“”;
如果(this.Weeks==1)
{
str+=此.Weeks+“week”;
}
否则如果(本周>1)
{
str+=这个.Weeks+“Weeks”;
}
如果(this.Days==1)
{
str+=this.Days+“day”;
}
否则如果(this.Days>1)
{
str+=this.Days+“Days”;
}
如果(this.Hours==1)
{
str+=此.Hours+“hour”;
}
否则,如果(this.Hours>1)
{
str+=这个.Hours+“Hours”;
}
//仅在持续时间小于一天时写入分钟
如果(this.Weeks==0&&this.Days==0&&this.Minutes>0)
{
str+=this.Minutes+“Minutes”;
}
返回str;
}
}
}

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