Resolved How to compare a date from a txt file with the date of windows

Post number 1 has been selected as best answer.

Steve90

New member
Joined
May 5, 2020
Messages
4
Programming Experience
Beginner
Hi,
I have a txt file like this:
21/05/2020 17:05:00 ; info ; info ; info
I know how to get this part of the first column "21/05/2020 17:05:00" and put it as a variable.
Now I have the problem that I don't know how to compare this date and hour that is in my txt file with the date and the hour of windows.. and if it don't mach I need to get a message.
Can someone tell how the code should be?

Thank you.

This is what I'm trying at moment:

C#:
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 dateString = date;
DateTime dateValue = DateTime.ParseExact(dateString, "dd/MM/yyyy HH:mm:ss", CultureInfo.CurrentCulture); <-- but here it give me an error : ""String not recognized as a valid DateTime value.""
I saw that, in the debug mode, in the variable "date" he read "03/04/2019 11:22:09 --- \t "

Do someone know to fix this or how to implement it with what I need?
 
Last edited by a moderator:

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
683
Location
Norway
Programming Experience
10+
ParseExact expects the input string to match the format exactly, if it contains extra text it will not match. Since you expect 19 characters you can remove the extra text by that length:
C#:
dateString = dateString.Remove(19);
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,860
Location
Sydney, Australia
Programming Experience
10+
I'm not sure whether those dashes you showed are literal text or not. You could call Trim or TrimEnd to remove any whitespace characters, which includes spaces and Tab characters. If you want to remove non-whitespace too then do as @JohnH suggested.
 

Steve90

New member
Joined
May 5, 2020
Messages
4
Programming Experience
Beginner
ParseExact expects the input string to match the format exactly, if it contains extra text it will not match. Since you expect 19 characters you can remove the extra text by that length:
C#:
dateString = dateString.Remove(19);
You were right! It worked as well with your solution.
Now I'm trying to compare what there is in the variable dateString (21/05/2020 17:05:00) with the windows date and hour.
C#:
DateTime dateValue = DateTime.ParseExact(dateString, "dd/MM/yyyy HH:mm:ss", CultureInfo.CurrentCulture);

if (dateValue.Date == DateTime.Now.Date)
{
    MessageBox.Show("OK");
}
else
{
    MessageBox.Show("BAD");
}
As I'm doing he just compare the data and not the hour, how can compare the variable dateString with the windows date and hour? It should be better if it don't look at milliseconds.
Can please help me?
 
Last edited by a moderator:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,227
Location
Virginia Beach, VA
Programming Experience
10+
The Date is just the date part of the the DateTime structure. Since that's the only thing you are telling the computer to compare, then that is exactly what it will do. Our current generation of computers and compilers don't have the capability of reading our minds yet to do what we mean rather than what we tell it to do. :)

If you look at the documentation, you'll see that the DateTime structure also has the Hour property.

I would urge you to exercise caution when doing comparisons of just parts of a DateTime, specially when you have to deal with multiple time zones. Either confirm that the two date times you are comparing are in the same time zone, or convert all off them to UTC before doing your comparisons.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,227
Location
Virginia Beach, VA
Programming Experience
10+
I'll also throw out the brute force approach as something that will work (assuming correct time zones), but will likely get you listed in The Daily WTF:
Code:
string format = "dd/MM/yyyy HH";
if (dateValue.ToString(format) == DateTime.Now.ToString(format))
:
 
Top Bottom