Languages
[Edit]
EN

C# / .NET - convert DateTime to DateTimeOffset

1 points
Created by:
Elleanor-Williamson
320

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
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

C# - DateTime conversion

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join