Languages
[Edit]
EN

C# / .NET - convert DateTime to iso 8601

14 points
Created by:
Trenton-McKinney
618

In this article, we are going to show how in C# / .NET convert DateTime object to iso 8601 string.

Quick solution:

Output:

 

Look to the below to see full examples:

1. Standard date and time format strings example

Edit

It is importnant to use invariant culture info to get proper formatted time from ToString() method.

Output:

2. Custom date and time format strings example

Edit

There are different formats of iso 8601 times.

Output:

Note: date and time string with Z suffix should be based on DateTime.UtcNow. Check this link for more details.

Other custom formats:

3. Custom week and ordinal date example

Edit

In this section, custom method that returns a week or ordinal date is presented.

Output:

Note: above example returns different week number depending of Calendar.GetWeekOfYear() method configuration.

 

See also

Edit
  1. ISO 8601 - What is Z in date time format yyyy-MM-ddTHH:mm:ssZ

References

Edit
  1. ISO 8601 - Wikipedia
  2. ISO week date - Wikipedia
  3. Standard Date and Time Format Strings - Microsoft Docs
  4. Custom date and time format strings - Microsoft Docs
1
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