19

Quartz.net offers a method to get the next time of the next trigger event: http://quartznet.sourceforge.net/apidoc/1.0/html/html/cc03bb79-c0c4-6d84-3d05-a17f59727c98.htm

The docs claim that this Trigger.GetNextFireTimeUtc() method return a DateTime? but it actually returns a DateTimeOffset?. I don't really get what DateTimeOffset is for or why this function return one instead of a regular DateTime. All I want is the next time the trigger is going to run but in my timezone.

I did this trigger.GetNextFireTimeUtc().Value.DateTime but it gave me a time 2 hours early, i.e. the UTC time. How can I get the correct time according to my computer?

Dan
  • 45,079
  • 17
  • 88
  • 157
  • Use the `LocalDateTime`. You should have a [look][1] [1]: http://stackoverflow.com/questions/179940/convert-utc-gmt-time-to-local-time – TryToSolveItSimple Oct 10 '14 at 09:43
  • You may be interested in [DateTime vs DateTimeOffset](http://stackoverflow.com/questions/4331189/datetime-vs-datetimeoffset). – Matt Johnson-Pint Oct 10 '14 at 18:04
  • 1
    Also, you are looking at Quartz's 1.0 docs. Quartz 2.0 changed this method to return a `DateTimeOffset?`, [as shown here](http://quartznet.sourceforge.net/apidoc/2.0/html/html/e07a781b-39c8-db14-3421-c563497fe23c.htm). – Matt Johnson-Pint Oct 10 '14 at 18:08

3 Answers3

47

You can just use the DateTimeOffset.LocalDateTime property:

trigger.GetNextFireTimeUtc().Value.LocalDateTime

From the documentation:

If necessary, the LocalDateTime property converts the current DateTimeOffset object's date and time to the local system's date and time. The conversion is a two-step operation:

  • The property converts the current DateTimeOffset object's time to Coordinated Universal Time (UTC).
  • The property then converts UTC to local time.

You should really look into DateTimeOffset though - it's an important type to understand if you're using the BCL for date/time work.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Why was this answer so hard to find? The M$ Docs on this are evil!!! [https://learn.microsoft.com/en-us/dotnet/standard/datetime/converting-between-datetime-and-offset](https://learn.microsoft.com/en-us/dotnet/standard/datetime/converting-between-datetime-and-offset) – wruckie Oct 13 '20 at 15:04
0

I bumped to this problem and none of these was real answer (by this question title). Solution examples for question title:

var myDateTimeOffset = (DateTimeOffset)DateTime.UtcNow;
var ans1 = myDateTimeOffset.DateTime.ToLocalTime();
var ans2 = myDateTimeOffset.DateTime.ToLocalTime().ToLocalTime(); // ans1==ans2

.NET5 C# 9.0

enter image description here

Vinigas
  • 464
  • 5
  • 18
-2

This code is to convert utc to local

var local = utc.ToLocalTime();
gunr2171
  • 16,104
  • 25
  • 61
  • 88
Mikant
  • 299
  • 3
  • 18
  • That's *just* going to change the `DateTime`'s kind, without changing its value at all. If the OP is simply displaying the value, then they wont see any change – Jon Skeet Oct 10 '14 at 09:41
  • The OP is indeed simply displaying the value – Dan Oct 10 '14 at 09:42