Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# DateTimeOffset formatting to a certain format

Tags:

c#

Is there any reference where I could find out how I can create a format for the DateTimeOffset that would enable me to produce a string like this?

2016-10-01T06:00:00.000000+02:00

I have a DateTimeOffset that I can work with, but I'm not sure how I could format it to produce the above string representation?

like image 600
joesan Avatar asked Feb 06 '15 11:02

joesan


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What language is C in?

C is a procedural language that provides no support for objects and classes. C++ is a combination of OOP and procedural programming languages. C has 32 keywords and C++ has 63 keywords. C supports built-in data types, while C++ supports both built-in and user-defined data types.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

What you want is an ISO 8601-standardized combined Date and Time string.

The "o" format string provides you with just that:

DateTimeOffset dto = new DateTimeOffset(DateTime.Now);
string iso8601date = dto.ToString("o")
like image 104
Mathias R. Jessen Avatar answered Oct 29 '22 17:10

Mathias R. Jessen


The "O" or "o" standard format specifier corresponds to the yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK custom format string for DateTime values and to the yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffzzz custom format string for DateTimeOffset values.

DateTime lDat = new DateTime(2009, 6, 15, 13, 45, 30, DateTimeKind.Local);

Console.WriteLine("{0} ({1}) --> {0:O}\n", lDat, lDat.Kind);
// 6/15/2009 1:45:30 PM (Local) --> 2009-06-15T13:45:30.0000000-07:00 

DateTimeOffset dto = new DateTimeOffset(lDat);
Console.WriteLine("{0} --> {0:O}", dto);
// 6/15/2009 1:45:30 PM -07:00 --> 2009-06-15T13:45:30.0000000-07:00    

Reference: https://msdn.microsoft.com/en-us/library/az4se3k1%28v=vs.110%29.aspx#Roundtrip

like image 23
Nikolay Kostov Avatar answered Oct 29 '22 18:10

Nikolay Kostov