Question

I am using Quartz.NET(http://quartznet.sourceforge.net/) and I ma trying to set start date at midnight. But I have problem with creating this date. This application will be standing on different servers in different timezones. But I want this time to be always set to midnight at Pacific Standard Time.

Here is my not working code:

    TimeZoneInfo info = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");       
    DateBuilder dateBuilder = DateBuilder.NewDateInTimeZone(info).AtHourMinuteAndSecond(0,0,0);           
    DateTimeOffset runTime = dateBuilder.Build();          

    IJobDetail job = JobBuilder.Create<TimeJob>()
        .WithIdentity("job1", "group1")
        .Build();

    ITrigger trigger = TriggerBuilder.Create()
        .WithIdentity("trigger1", "group1")
        .StartAt(runTime)
        .WithSimpleSchedule(x => x.WithIntervalInHours(24).RepeatForever())
        .Build();

I need to have date in DateTimeOffset format that would be midnight Pacific Standard Time. I ma using MVC 3. Any help much appreciated!

Was it helpful?

Solution

This should give you the midnight PST date irrespective of the servers.

DateTime dateInDestinationTimeZone = System.TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, System.TimeZoneInfo.Utc.Id, "Pacific Standard Time").Date;

OTHER TIPS

Sorry to resurrect this from the dead, but I found the solution provided by @Vishnu didn't work for me.

I am working on an EST machine, and the result of the created object, was midnight in my local time. Also, the solution mentioned didn't create a DateTimeOffset object, but a DateTime one.

I finally found a solution that worked for me, figured I'd share it here since someone else might find it useful.

var userTimeZone =
        TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var startTime = new DateTimeOffset(DateTime.Today.Ticks,
        userTimeZone.GetUtcOffset(DateTime.Today) );

The trick here is using Ticks, which makes calculation very 'seamless'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top