时间 - 一次两次差异

时间:2017-04-18 13:36:50

标签: c# time msdn

我对此代码有疑问。我会检查两次是否有低于7秒的差异。

static void Main(string[] args)
{
    List<DateTime> logDates = new List<DateTime>();

    //Define regex string
    string pattern = @"(?<logDate>(\d){4}-(\d){2}-(\d){2}\s(\d){2}:(\d){2}:(\d){2})";            
    Regex reg = new Regex(pattern);    

    try
    {   // Open the text file using a stream reader.
        using (StreamReader sr = new StreamReader("C:\\test.txt"))
        {
            // Read the stream to a string, and write the string to the console.
            String logContent = sr.ReadToEnd();
            Console.WriteLine(logContent);

            //run regex
            MatchCollection matches = reg.Matches(logContent);

            //iterate over matches
            foreach (Match m in matches)
            {
                DateTime logTime = DateTime.Parse(m.Groups["logDate"].Value);
                //logDates.Add(logTime);                        
                Console.WriteLine("TIME:" + logTime.TimeOfDay);
            }
    #if DEBUG
            Console.WriteLine("Press enter to close...");
            Console.ReadLine();
    #endif
        }
    }
    catch (Exception e)
    {
        Console.WriteLine("The file could not be read:");
        Console.WriteLine(e.Message);
    }
}

此代码正确打开txt文件(test.txt),读取日期并打印到控制台。

我的问题是:如何检查两次(TWO AT ONCE)是否有低于7秒的差异?

编辑:如果我有消息要说它没关系,那就更好了。

此致

7 个答案:

答案 0 :(得分:1)

根据您的问题,我假设您正在尝试检查两个DateTime之间的差异是否低于7秒。这是我希望可能有所帮助的事情。

    static void Main(string[] args)
    {
        TimeSpan span = new TimeSpan(0, 0, 0, 7, 0);

        //Your array of DateTimes
        DateTime[] dateTimes = new DateTime[]
        {
            new DateTime(2017, 04, 18, 0, 0, 0),
            new DateTime(2017, 04, 18, 0, 0, 7),
            new DateTime(2017, 04, 18, 0, 0, 15),
            new DateTime(2017, 04, 18, 0, 0, 21),
        };

        //Check through whole array of DateTimes, in sequence
        for (int i = 0; i < dateTimes.Count() - 1; i++)
        {
            if (dateTimes[i + 1] - dateTimes[i] <= span)
            {
                Console.WriteLine("OK");
            }
            else
            {
                Console.WriteLine("NOT OK");
            }
        }
        //Output of this example:
        //OK
        //NOT OK
        //OK

答案 1 :(得分:0)

您可以使用DateTime.Subtract(DateTime)来提供TimeSpan对象,并且可以使用其属性TotalSeconds来确定两个日期之间的差异。

答案 2 :(得分:0)

我刚刚遇到这个问题并在Stack上找到了一些代码,但是对于我的生活找不到原始的创建者。我发布了修改过的代码,但是如果有人能够识别原作者,请告诉我,我会确保将其归功于他们。使用下面的代码,您可以输入两个日期并获得一些比较它们的选项。

using System;

namespace BFSShared
{
    /// <summary>
    /// </summary>
    public struct DateTimeSpan
    {
        private readonly int years;
        private readonly int months;
        private readonly int days;
        private readonly int hours;
        private readonly int minutes;
        private readonly int seconds;
        private readonly int milliseconds;

        /// <summary>
        /// Initializes a new instance of the <see cref="DateTimeSpan"/> struct.
        /// </summary>
        /// <param name="years">The years.</param>
        /// <param name="months">The months.</param>
        /// <param name="days">The days.</param>
        /// <param name="hours">The hours.</param>
        /// <param name="minutes">The minutes.</param>
        /// <param name="seconds">The seconds.</param>
        /// <param name="milliseconds">The milliseconds.</param>
        public DateTimeSpan(int years, int months, int days, int hours, int minutes, int seconds, int milliseconds)
        {
            this.years = years;
            this.months = months;
            this.days = days;
            this.hours = hours;
            this.minutes = minutes;
            this.seconds = seconds;
            this.milliseconds = milliseconds;
        }

        /// <summary>
        /// Gets the years.
        /// </summary>
        /// <value>
        /// The years.
        /// </value>
        public int Years { get { return years; } }
        /// <summary>
        /// Gets the months.
        /// </summary>
        /// <value>
        /// The months.
        /// </value>
        public int Months { get { return months; } }
        /// <summary>
        /// Gets the days.
        /// </summary>
        /// <value>
        /// The days.
        /// </value>
        public int Days { get { return days; } }
        /// <summary>
        /// Gets the hours.
        /// </summary>
        /// <value>
        /// The hours.
        /// </value>
        public int Hours { get { return hours; } }
        /// <summary>
        /// Gets the minutes.
        /// </summary>
        /// <value>
        /// The minutes.
        /// </value>
        public int Minutes { get { return minutes; } }
        /// <summary>
        /// Gets the seconds.
        /// </summary>
        /// <value>
        /// The seconds.
        /// </value>
        public int Seconds { get { return seconds; } }
        /// <summary>
        /// Gets the milliseconds.
        /// </summary>
        /// <value>
        /// The milliseconds.
        /// </value>
        public int Milliseconds { get { return milliseconds; } }

        private enum Phase { Years, Months, Days, Done }

        /// <summary>
        /// Compares the dates.
        /// </summary>
        /// <param name="date1">The date1.</param>
        /// <param name="date2">The date2.</param>
        /// <returns></returns>
        public static DateTimeSpan CompareDates(DateTime date1, DateTime date2)
        {
            if (date2 < date1)
            {
                var sub = date1;
                date1 = date2;
                date2 = sub;
            }

            var current = date1;
            var years = 0;
            var months = 0;
            var days = 0;

            var phase = Phase.Years;
            var span = new DateTimeSpan();
            var officialDay = current.Day;

            while (phase != Phase.Done)
            {
                switch (phase)
                {
                    case Phase.Years:
                        if (current.AddYears(years + 1) > date2)
                        {
                            phase = Phase.Months;
                            current = current.AddYears(years);
                        }
                        else
                        {
                            years++;
                        }
                        break;
                    case Phase.Months:
                        if (current.AddMonths(months + 1) > date2)
                        {
                            phase = Phase.Days;
                            current = current.AddMonths(months);
                            if (current.Day < officialDay && officialDay <= DateTime.DaysInMonth(current.Year, current.Month))
                                current = current.AddDays(officialDay - current.Day);
                        }
                        else
                        {
                            months++;
                        }
                        break;
                    case Phase.Days:
                        if (current.AddDays(days + 1) > date2)
                        {
                            current = current.AddDays(days);
                            var timespan = date2 - current;
                            span = new DateTimeSpan(years, months, days, timespan.Hours, timespan.Minutes, timespan.Seconds, timespan.Milliseconds);
                            phase = Phase.Done;
                        }
                        else
                        {
                            days++;
                        }
                        break;
                    case Phase.Done:
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
            }
            return span;
        }
    }
}

原始代码可以在这里找到, Difference in months between two dateshttps://stackoverflow.com/users/189950/kirk-woll

撰写

答案 3 :(得分:0)

以下是使用DateTime.Subtract

的示例
DateTime datetime1 = DateTime.Now;
DateTime datetime2 = DateTime.Now;

if (datetime2.Subtract(datetime1).TotalSeconds < 7)
{
    Console.WriteLine("Less than 7 seconds");
}

不确定其他答案是否为您解决了这个问题。

更新了用于比较滚动列表中的2个日期的代码。 因此,检查A [1] -A [0]低于7秒或A [2] -A [1]低于7秒

//mock of your list collection
  List<DateTime> dates = new List<DateTime>()
  {
      {DateTime.Now},
      {DateTime.Now.AddSeconds(8)},
      {DateTime.Now.AddSeconds(18)},
      {DateTime.Now.AddSeconds(28)},
      {DateTime.Now.AddSeconds(30)},
   };

   //tempoary store the previous 3 dates
   List<DateTime> dates2 = new List<DateTime>();

   foreach (var item in dates)
   {
       dates2.Add(item);
       if (dates2.Count > 2)
       {
           //Check if either dates2[0] & dates2[1] and dates2[1] and dates[2] are 7 seconds apart
           if (dates2.Zip(dates2.Skip(1), (x, y) => y.Subtract(x))
               .Any(x => x.TotalSeconds < 7))
           {
               Console.WriteLine("OK");
           }

          //remove the first element so only 3 dates in the temporary list
          dates2.RemoveAt(0);
       }
}

答案 4 :(得分:0)

您需要比较所有日志日期。这将更容易&#34;如果您先订购商品,然后找到差异小于7秒的第一个日期,并将它们和其余商品退回。

var logDates = new List<DateTime>(); // loaded from file

var orderedLogDates = logDates.OrderBy(logDate => logDate).ToList();
var lowIndex = 0;
var upperIndex = orderedLogDates.Count - 1;
while (lowIndex < upperIndex)
{
    var diff = (orderedLogDates[upperIndex] - orderedLogDates[lowIndex]).TotalSeconds;
    if (diff < 7)
    {
        // Here we can return all items between lower and upper indexes
        var amountToReturn = upperIndex - lowIndex;
        return orderedLogDates.GetRange(lowIndex, amountToReturn);
    }
    lowIndex++;
    upperIndex--;
}
return new List<DateTime>(); // empty list if not found

答案 5 :(得分:0)

在循环中,您可以使用已读取的日期检查每个日期,并将它们保存在列表中。

foreach (Match m in matches)
{
    DateTime logTime = DateTime.Parse(m.Groups["logDate"].Value);
    bool alreadyExistsLessThanSevenSeconds = 
        logDates.Any(dateTime => Math.Abs((dateTime - currentDateTime).TotalSeconds) <= 7);

    if (alreadyExistsLessThanSevenSeconds)
    {
        // Exists within the seven seconds range
    }
    {
        // Doesnt exists within the seven seconds range
    }
    logDates.Add(logTime);

    Console.WriteLine("TIME:" + logTime.TimeOfDay);
}

答案 6 :(得分:0)

如果您将所有日期时间放入集合中,则可以使用此LINQ查找在另一时间内7秒内的日期。

首先确保所有时间都是有序的,然后检查列表中的上一个时间,看看它们的差异是否小于7秒。

这是一个例子......

DateTime d1 = DateTime.Now;
DateTime d2 = DateTime.Now.AddSeconds(5);
DateTime d3 = DateTime.Now.AddSeconds(15);
DateTime d4 = DateTime.Now.AddSeconds(30);
DateTime d5 = DateTime.Now.AddSeconds(32);

List<DateTime> times = new List<DateTime>() { d1, d2, d3, d4, d5 };

var withinSeven = times.OrderBy(t => t)
    .Where((t, i) =>
        i > 0 && t.Subtract(times[i - 1]).Seconds < 7)
    .ToList();

foreach (var time in withinSeven)
    Console.WriteLine(time);