区分时间跨度在上午8点到9点到晚上8点到晚上9点等

时间:2015-05-08 12:27:13

标签: c# .net datetime calendar timespan

我正在寻找一种区分时间跨度的方法,例如,在上午8点到9点到晚上8点到晚上9点,上午10点到12点,晚上10点到12点等等。

由于时间是相对的,我正在寻找一种创造性的方法来实现这一目标。

这是我到目前为止所尝试的内容:

// 3:41pm - 4:41pm

DateStart: 5/7/2015 3:41:41 PM
DateExpire: 5/7/2015 4:41:41 PM

DateExpire.Subtract(DateStart): 01:00:00
ToUnixEpocTime(DateStart): 1431013301
ToUnixEpocTime(DateExpire): 1431016901
int timespan = ToUnixEpocTime(DateExpire) - ToUnixEpocTime(DateStart);
timespan: 3600


// 4:41pm - 5:41pm
DateStart: 5/7/2015 4:41:41 PM
DateExpire: 5/7/2015 5:41:41 PM
DateExpire.Subtract(DateStart): 01:00:00
ToUnixEpocTime(DateStart): 1431016901
ToUnixEpocTime(DateExpire): 1431020501
int timespan = ToUnixEpocTime(DateExpire) - ToUnixEpocTime(DateStart);
timespan: 3600

......我不确定为什么这些结果让我感到惊讶,因为它们才有意义 - 因为它只是基本的减法。

3 个答案:

答案 0 :(得分:2)

如前所述,TimeSpan是时间间隔。时间间隔上午8点到9点和下午8点到晚上9点是完全相同的 - 它是1小时。如果你想区分它们,那么创建你自己的类,它将保存每个间隔的开始和结束时间,并使用这些值来比较对象:

public class DateRange
{
    public DateRange(DateTime start, DateTime end)
    {
        if (end < start)
           throw new ArgumentException("end");

        Start = start;
        End = end;
    }

    public DateTime Start { get; private set; }
    public DateTime End { get; private set; }
    public TimeSpan Duration { get { return End - Start; }}

    public override bool Equals(object obj)
    {
        DateRange other = obj as DateRange;
        if (other == null)
           return false;

        return Start == other.Start && End == other.End;
    }
    // override GetHashCode
}

现在,如果您有两个日期范围

var morningRange = new DateRange(8amTime, 9amTime);
var eveningRange = new DateRange(8pmTime, 9pmTime);

它们不会相同,但持续时间会相同

morningRange.Duration == eveningRange.Duration // true
morningRange.Equals(eveningRange) // false

您还可以定义==!=运算符

public static bool operator== (DateRange x, DateRange y)
{
    if (Object.ReferenceEquals(x, y))
        return true;

    if (((object)x == null) || ((object)y == null))
        return false;

    return x.Equals(y);
}

public static bool operator !=(DateRange x, DateRange y)
{
    return !(x == y);
}

现在比较更容易

morningRange == eveningRange // false
morningRange != eveningRange // true

答案 1 :(得分:0)

我的解决方案:

public class TimeRange
{
    private double _begin;              // total ms from 00:00
    private double _end;                // total ms from 00:00

    /// <param name="begin"> begin time (for example 8:30:15am) </param>
    /// <param name="end"> begin time (for example 12pm) </param>
    public TimeRange(DateTime begin, DateTime end)
    {
        _begin = begin.TimeOfDay.TotalMilliseconds;
        _end = end.TimeOfDay.TotalMilliseconds;
    }

    /// <summary>
    /// check time in range
    /// </summary>
    /// <param name="time"> time </param>
    /// <returns></returns>
    public bool IsDateInRange(DateTime time)
    {
        double value = time.TimeOfDay.TotalMilliseconds;
        return _end > _begin ? (value >= _begin && value <= _end) : (value >= _end && value <= _begin);
    }

    public override string ToString()
    {
        DateTime today = DateTime.Today;
        return string.Format(_end > _begin ? "{0}-{1}" : "{1}-{0}",
            (today + TimeSpan.FromMilliseconds(_begin)).ToString("HH:mm"),
            (today + TimeSpan.FromMilliseconds(_end)).ToString("HH:mm"));
    }
}

用法:

TimeRange range = new TimeRange(DateTime.Today, DateTime.Now);
Console.WriteLine("Range is {0}", range.ToString());
for (int i = 0; i < 24; i++)
{
    Console.WriteLine("Time {0:00}:00 is in range? {1}", i, range.IsDateInRange(DateTime.Today.AddHours(i)));
}

输出:

Range is 00:00-17:45
Time 00:00 is in range? True
Time 01:00 is in range? True
Time 02:00 is in range? True
Time 03:00 is in range? True
Time 04:00 is in range? True
Time 05:00 is in range? True
Time 06:00 is in range? True
Time 07:00 is in range? True
Time 08:00 is in range? True
Time 09:00 is in range? True
Time 10:00 is in range? True
Time 11:00 is in range? True
Time 12:00 is in range? True
Time 13:00 is in range? True
Time 14:00 is in range? True
Time 15:00 is in range? True
Time 16:00 is in range? True
Time 17:00 is in range? True
Time 18:00 is in range? False
Time 19:00 is in range? False
Time 20:00 is in range? False
Time 21:00 is in range? False
Time 22:00 is in range? False
Time 23:00 is in range? False

答案 2 :(得分:0)

只是给出一个想法

如果两者都是AM或PM那么
添加12个时差
否则
只是减去

例如,

// 3:41 pm - 4:41 pm DateStart:5/7/2015 3:41:41 PM
DateExpire:5/7/2015 4:41:41 PM

if(DateStart = PM和DateExpire = PM)
timeSpan =(DateStart - DateExpire)+ 12
否则
timeSpan =(DateStart - DateExpire)