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.
To compare two different
DateTimewith an accuracy of second, the easiest approach would beI have used the absolute value, so in case for any reason you swap the order of the
datetimevalues in the subtraction line the result will not be affected.The condition to consider two
DateTimevalues 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
DateTimeis not dependent on the servers timezone. In order to use it you can writeinstead of
Before you do so, confirm that your
DateTimevalues in the file is in UTC as well.