Search code examples
c#datetime-comparison

How do I compare date and hour from a variable and date and hour of Windows?


I made this code:

string[] liness = File.ReadAllLines(ofd.FileName);

if (liness.Length > 0)
{
    string lastLine = liness[liness.Length - 1];
    string[] columns = lastLine.Split(';');
    if (columns.Length > 0)
    {
        string date = columns[0];
        //string lastColumn = columns[columns.Length - 5];
        //ReaderRichTxtBox.Text = date;

        string dateString = date;
        dateString = dateString.Remove(19);

        DateTime dateValue = DateTime.ParseExact(dateString, "dd/MM/yyyy HH:mm:ss", CultureInfo.CurrentCulture);
        if (dateValue.Date.TimeOfDay == DateTime.Now.Date.TimeOfDay)
        {
            MessageBox.Show("OK");
        }
        else
        {
            MessageBox.Show("BAD");
        }

I need to get last row and the first column of a txt file.

The txt file is something like this:

21/05/2020 17:05:00 ; info ; info ; info

and I need just the 21/05/2020 17:05:00. I've done this part as you can see.

Now I have the variable dateString that contains the date and hour of my txt file and I need to compare it with date and hours of Windows. I tried it as you can see, but it compare just the date and not the hours.

How can I compare the date and the hour of my txt file with date and hour of Windows? How should the code be? It is better for me if it doesn't consider the milliseconds.


Solution

  • To compare two different DateTimewith an accuracy of second, the easiest approach would be

    string[] liness = File.ReadAllLines(ofd.FileName);
    
    if (liness.Length > 0)
    {
        string lastLine = liness[liness.Length - 1];
        string[] columns = lastLine.Split(';');
        if (columns.Length > 0)
        {
            string date = columns[0];
            //string lastColumn = columns[columns.Length - 5];
            //ReaderRichTxtBox.Text = date;
            string dateString = date;
            dateString = dateString.Remove(19);
    
            var dateTimeFromFile = DateTime.ParseExact(dateString, "dd/MM/yyyy HH:mm:ss", CultureInfo.CurrentCulture);
            var now = DateTime.UtcNow;
    
            var timeDifference = now - dateTimeFromFile;
            if (Math.Abs(timeDifference.TotalSeconds) < 1)
            {
                MessageBox.Show("OK");
            }
            else
            {
                MessageBox.Show("BAD");
            }
    

    I have used the absolute value, so in case for any reason you swap the order of the datetime values in the subtraction line the result will not be affected.

    Math.Abs(timeDifference.TotalSeconds) < 1
    

    The condition to consider two DateTime values same, is their difference to be below 1 second. The reason is that we need to take into account some rounding +-500 milliseconds.

    UTC Time

    It would be better to use UTC (Coordinated Universal Time) so your DateTime is not dependent on the servers timezone. In order to use it you can write

    var now = DateTime.UtcNow;
    

    instead of

    var now = DateTime.Now;
    

    Before you do so, confirm that your DateTime values in the file is in UTC as well.