Akang Toshi
Akang Toshi

Reputation: 191

hangfire BackgroundJob.Schedule is running on server time only. How do I fix this?

I am scheduling a task on hangfire but,it doesnt trigger at my local time(India Standard Time). If i set it to say 2030/3/3 10:00:00 am it will trigger only at the server time which has a 5 hour and 30 min differnce and Not at the right time i want.

i have tried doing something like

DateTime updatedDate = DateTime.Parse(datetime);
updatedDate = updatedDate.AddSeconds(+30);// add extra 30 sec
DateTime currentTime = TimeZoneInfo.ConvertTime(updatedDate, TimeZoneInfo.FindSystemTimeZoneById("India Standard Time"));
string jobid = BackgroundJob.Schedule<Tasks>(x => x.recurringService(), currentTime).ToString();

but it doesnt work at all. Someone please show me a way.

Upvotes: -1

Views: 490

Answers (1)

Slack Groverglow
Slack Groverglow

Reputation: 856

I'm assuming your computer is set to India Standard Time (IST). I'm also assuming that datetime string gives no indication of time-zone.

After DateTime.Parse(datetime), variableupdateDate should have its 'kind' equal to DateTimeKind.Unspecified. See Console.Write(updateData.Kind)

As per Microsoft docs, If the parameter passed TimeZoneInfo.ConvertTime is DateTimeKind.Unspecified, it assumes DateTimeKind.Local (which is IST). This means ConverTime is converting from IST to IST so no change will be made.

For this to work, you need the updateDate to be DateTimeKind.Utc

DateTime.SpecifyKind(updatedDate, DateTimeKind.Utc);

Upvotes: 1

Related Questions