Check if x months have passed in asp.net mvc

I have a budgeting app that sets a budget for n months, how can the app know if n months have passed and not use the budget? How does mvc automatically check?

+3


source to share


3 answers


Steve's answers above make a simple example of this. If you want to do this automatically, you have to put it in some services and call it from the scheduler or some services like Hangfire or Quartz .

(DateTime.Now - StartDate)

      



If this check has something to do with sending email notification, etc., I prefer to check it from the database and run a SQL job to run it every day, or anytime you think it fits.

+3


source


Assuming you are storing the budget start date:

(DateTime.Now - StartDate)



Gives you the result as a Timespan from which you can get time in multiple formats.

+1


source


You can use it asynchronously using a timer on the server. This would mean that the service must remain for x months. The best choice would most likely be to store the end date and start a timer to call the function when that date condition is true.

//Timer set to run every 20 seconds    
Timer = new System.Timers.Timer(20000);
            //Timer set to run every 20 seconds
            Timer.Elapsed += SendTimerElapsed;
            Timer.Start();

      

Here's an example:

 class Program
{
    static void Main(string[] args)
    { 
        Timer t = new System.Timers.Timer(1000);
        t.Elapsed += Hitme;
        t.Start();
        Console.ReadLine();
    }

    private static void Hitme(object sender, ElapsedEventArgs e)
    {
        Console.WriteLine("Ouch");
    }
}

      

0


source







All Articles