1

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.

John Vasquez
  • 115
  • 1
  • 8
  • 2
    When you say "the user sets DPIYMO to November" do you mean "11" since the field is an integer and cannot be set to "November"? – samyap Dec 17 '19 at 17:26
  • Yes @samyap that is correct – John Vasquez Dec 17 '19 at 17:27
  • So you're just asking how to get the current month as an integer? – Rup Dec 17 '19 at 17:28
  • @Rup essentially, I was not sure what was the more efficient way. I just need a check as to comapre the integer in DPIYRMO vs what the current month is, something like a datetime.now.month() – John Vasquez Dec 17 '19 at 17:33
  • 1
    https://stackoverflow.com/questions/6765441/how-to-get-complete-month-name-from-datetime Use that link if you want the string value of the month and then use: `var monthAsInt = DateTime.Now.Month` if you want the value as an integer. – samyap Dec 17 '19 at 17:34

1 Answers1

2

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.

Guillaume S.
  • 1,515
  • 1
  • 8
  • 21