C# - Convert unix timestamp to DateTime or DateTimeOffset

The easiest way to Convert a Unix timestamp to a DateTime or DateTimeOffset is to use the methods FromUnixTimeSeconds for seconds or FromUnixTimeMilliseconds for milliseconds. However this method only exists on the DateTimeOffset struct, however you can easily get a DateTime from a DateTimeOffset by calling the .DateTime or .UtcDateTime method on your DateTimeOffset instance.

An example of FromUnixTimeSeconds - and what you are likely looking for - can be seen here:

DateTimeOffset dateTimeOffset = 

Here is an example of FromUnixTimeMilliseconds:

DateTimeOffset dateTimeOffset = 

Here is an example of creating a DateTime based on a unix timestamp:

DateTime dateTimeOffset = 

If you want to get a unix timestamp from a DateTime or DateTimeOffset (the reverse of the above), check out my other post on how to do this in-depth. In short you can use .ToUnixTimeSeconds and .ToUnixTimeMilliseconds to achieve this.

I hope you found this post helpful, feel free to leave a comment down below!