Ask Your Question
1

How can I obtain a string representation of an ISO 8601 date from a DateTime object?

asked 2023-06-19 08:15:24 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-06-19 08:31:02 +0000

ladyg gravatar image

You can obtain a string representation of an ISO 8601 date from a DateTime object using the ToString() method with the "s" format specifier. This will return the date and time in the following format: "yyyy-MM-ddTHH:mm:ss" where "T" is the separator between the date and time.

For example:

DateTime now = DateTime.Now;
string isoDate = now.ToString("s");
Console.WriteLine(isoDate);

Output:

2021-07-06T10:46:23

If you want to include the time zone offset, you can use the "o" format specifier instead:

string isoDateWithOffset = now.ToString("o");
Console.WriteLine(isoDateWithOffset);

Output:

2021-07-06T10:46:23.6759678-04:00
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-19 08:15:24 +0000

Seen: 15 times

Last updated: Jun 19 '23