Search code examples
c#asp.netschedulerhangfire

set a specific Date And Time in HangFire


How do I set a specific time? for example 7/2/2021 8:00 AM.
Which methods should I use? Enqueue methods or Schedule methods or AddOrUpdate Methods?


Solution

  • Hangfire uses cron schedule expressions. You can set any date-time expression you want. for example

    0 8 24 7 * => “At 08:00 on day-of-month 24 in July.”

    the following code shows how to use the expression,You can use some online tools for creating expressions like crontab.

    RecurringJob.AddOrUpdate(jobId, methodCall, "0 8 24 7 *", TimeZoneInfo.Local);
    

    if you want to execute something once you should use BackgroundJob.

    var selectedDate = DateTimeOffset.Parse("2021-07-02 08:00:00");
    BackgroundJob.Schedule(() => YourDelayedJob(), selectedDate);