Home > Enterprise >  Display a warning alert from between date using C# ASP NET
Display a warning alert from between date using C# ASP NET

Time:06-05

For each month is compiled by registered users the customer satisfaction survey relating to the previous month, using C# and ASP NET.

E. g.:

This current month June, must be completed the previous month May customer satisfaction survey.

The month May survey must be completed from 01st June to 15th June.

From 16th June to 30th June I need to display an alert that the survey for the month of June will be available from July 1st.

Does this C# code seem correct to you?

string tMonthS = DateTime.Now.AddMonths(1).ToString("MMMM");
 
DateTime dt = DateTime.Now;
DateTime start = new DateTime(dt.Year, dt.Month, 1).AddDays(15);
DateTime end = start.AddMonths(1).AddDays(-16);
 
string startOfMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).ToString("dd/MM/yyyy");
 
if (dt == start && dt <= end)
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg",
        "alert('Alert! The survey for the month of "   tMonthS.ToString()   " "   DateTime.Now.Year   "\\n"  
        "available from the day "   startOfMonth.ToString()   "');", true);
}

CodePudding user response:

You probably want dt >= start in your conditional, since otherwise it will only match one day (and at midnight of that day's morning, too), and you don't want to do subtraction for the end value either, since that is not usable for all months; you should be able to get end with a chain of calculations from start.GetMonth().AddMonths(1) or similar. I wouldn't recommend actually coding it in one line like that, but that is the idea I think.

  • Related