John Vasquez
John Vasquez

Reputation: 115

How to compare int date with string by Month

I have the value DPIYRMO type: Int

enter image description here

My goal is to take this value and create an if statement where it compares the current month, to the month that the user sets DPIYRMO to.

Example, the user sets DPIYRMO to November, if this happens, I will have a messagebox that lets them know that their DPIYRMO is set to that month and not the current month.

This if statement will be placed in here:

private void OnPostCertificate()
        {
            if (TaxCertificateList.Where(c => c.IsSelected).Count() == 0)
                return;

            bw = new BackgroundWorker();
            bw.WorkerReportsProgress = true;
            bw.WorkerSupportsCancellation = true;

            bw.DoWork += new DoWorkEventHandler(bw_DoPost);
            bw.RunWorkerCompleted += BwOnRunPostCompleted;
            bw.RunWorkerAsync();
        }

I believe I may have to use substrings, however, I am not sure where to start.

Upvotes: 1

Views: 325

Answers (1)

Guillaume S.
Guillaume S.

Reputation: 1545

Don't you simply want something like that?

if (obj.DPIYRMO != DateTime.Now.Month)
{
    string dpiyrmoMonth = new DateTime(1970, obj.DPIYRMO, 1).ToString("MMMM");
    Console.WriteLine(dpiyrmoMonth + " does not match the current month.");
    // prints March does not match the current month.
}

DateTime.Now returns the current date and time, and the Month property returns the month as an integer: 1 for January, 2 for February... So you can compare it to your integer variable.

new DateTime(1970, obj.DPIYRMO, 1) returns a date with the month part equals to the one stored in your integer variable. Note that the year 1970 and the 1st day of the month are arbitrary values for the use we do, because ToString("MMMM") returns the month part formatted in a human-readable string. See the format you can use.

Upvotes: 2

Related Questions