0

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.

JeremyRock
  • 396
  • 1
  • 8
Steve
  • 5
  • 2

2 Answers2

0

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.

Stelios Giakoumidis
  • 2,153
  • 1
  • 7
  • 19
  • Please look at my answer, I don't understand what you are trying to tell me . – Steve May 24 '20 at 08:13
  • I'm a beginner and I need you to understand it better with my code, what I have to change. Yes in my file values are in UTC. – Steve May 24 '20 at 08:22
  • Could you mark the answer and upvote, it is a common issue for beginners. So the rest of the community will get helped next time? – Stelios Giakoumidis May 25 '20 at 08:08
  • ofc, I did it now =) – Steve May 25 '20 at 08:13
  • thanks! Although mark as best answer and upvote an answer are different things. One means worked for me, the other one means it is a helpful answer. – Stelios Giakoumidis May 25 '20 at 08:15
  • I tried to upvote but it say that I have <15 reputation and it will not change the publicly displayed post score :-? I marked this as the best answer. – Steve May 25 '20 at 08:22
0

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");
Stelios Giakoumidis
  • 2,153
  • 1
  • 7
  • 19
Steve
  • 5
  • 2
  • I have updated the snippet. The variables you mentioned, is only renaming so the names are more descriptive. If you want an accuracy of +-5 mins, then change Math.Abs(timeDifference.TotalSeconds) < 1 to Math.Abs(timeDifference.TotalMinutes) < 5. Let me know if you need more assistance – Stelios Giakoumidis May 24 '20 at 11:41