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

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:
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);
 
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.
 
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:
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.
 
If you want the date and time less the milliseconds, it makes sense to get the milliseconds and then subtract that from the DateTime. That's what you should research how to do.
 
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:
C#:
string format = "dd/MM/yyyy HH";
if (dateValue.ToString(format) == DateTime.Now.ToString(format))
:
 
Back
Top Bottom