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

115 Views Asked by At

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.

2

There are 2 best solutions below

6
Stelios Giakoumidis On BEST ANSWER

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.

1
Steve On

Sorry I I badly explained myself i need an accurancy of minutes, something like + - 5 min in the comparison. But apart from that here you create the variable now and set the date and hour of windows right? Why do you create dateTimeFromFile? I already have a string var where I store the date and the hour read from file. Now the code looks like below, but I did not understand as well what you said and it don't work

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


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

                    DateTime dateValue = DateTime.ParseExact(dateString, "dd/MM/yyyy HH:mm:ss", CultureInfo.CurrentCulture);
                    var now = DateTime.UtcNow;
                    var dateTimeFromFile = DateTime.Now.AddMilliseconds(10);
                    var timeDifference = now - dateTimeFromFile;

                    if (Math.Abs(timeDifference.TotalSeconds) <1)
                    {
                        Console.WriteLine("Same");
                    }
                    else
                    {
                        Console.WriteLine("Bad2");