Languages
[Edit]
EN

C# / .NET - convert DateTime to DateTimeOffset

1 points
Created by:
JustMike
3488

In C# / .NET it is possible to convert DateTime object DateTimeOffset in the following ways.

1. DateTimeOffset class casting example

DateTimeOffset now = (DateTimeOffset)DateTime.Now;

Console.WriteLine(now.ToString());

Output:

08/14/2019 23:41:58 +01:00
Note: this example has been run with independent culture.

2. DateTimeOffset class constructor example

DateTimeOffset now = new DateTimeOffset(DateTime.Now);

Console.WriteLine(now.ToString());

Output:

08/14/2019 23:47:04 +01:00
Note: this example has been run with independent culture.

References

  1. DateTime Structure Method - Microsoft Docs
  2. Converting between DateTime and DateTimeOffset - Microsoft Docs

C# - DateTime conversion

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.