I did not think that I would ask such a question, but I really cannot find a solution to format the current date in ISO 8601, setting the DateTime data type aside

Here is my code:

var datenow = DateTime.Now.ToString("o"); var dateTime2 = DateTime.Parse(datenow); 

1 - Is it impossible to immediately output DateTime in the right format, without translating it into a string ??

2 - Even if this option is the best (back there :)) DateTime.Parse returns the same format again.

2 answers 2

You are confusing DateTime and its string representation. Take, for example, the number 11 - it can be written as 0xB, 013, or there XI, but it nevertheless always remains a number.

The DateTime object itself does not have an output format, it is just an object with fields and methods. What you see is a string representation, and yes, when outputting, the object turns into a string, either explicitly (then you control the format) or implicitly (then the format does not depend on you).

Therefore, what you see as the “ DateTime value” is only its string representation, one of the possible. DateTime.Parse only turns the formatted string back into DateTime .

The answer to your question is negative: DateTime does not have a format, and you can only output it, really, by turning it into a string (either with a format or without it).

Like any other type, by the way .


The correct option for converting between the line and DateTime in both directions is:

 var lang = CultureInfo.InvariantCulture; // или CultureInfo.CurrentCulture, // смотря по тому, что вам нужно var format = "o"; var dt = DateTime.Now; var s = dt.ToString(format, lang); var dt2 = DateTime.ParseExact(s, format, lang); 
  • Well, how to turn a formatted string into a DateTime type without changing the format? - Aleman
  • 2
    @Aleman: Let's do it again. The DateTime object does not have a format . He does not know which format string will be displayed. The format only happens when turning into a string. - VladD
  • one
    @Aleman: DateTime.ParseExact ? - VladD
  • one
    @Aleman: What's unusual in your example? All as I wrote. In dt2 there is no inside output format. What did you expect instead? - VladD
  • 2
    @Aleman, DateTime.ParseExact обратно переводит в прежний формат - DateTime is, roughly speaking, just a number. In what format you will print it, you decide directly with this seal, setting the appropriate locale (the rules for outputting human-readable dates, times, currencies, etc. for a particular language). You can print the same DateTime both as дд.мм.гггг , and as мм/дд/гггг , and somehow differently - the internal format of the variable itself with the date will not change. - ߊߚߤߘ
  1. It is impossible. Unless you use a locale in which such a format is "preferred"; but since it is a technical format, it is not directly used in human reality. So such a locale hardly exists, since there is no region where it is relevant. But even if it existed, inside it would still be a conversion to a string. But you do not see this stage. He is inside.
  2. There is some misunderstanding here.

Suppose you have the string "FF" containing a hexadecimal representation of a number.

And you are trying to turn it into an integer value. You open the debugger and see with horror that the 16th is lost! Instead of the original FF, you see the decimal 255.

Panic. Perplexity

But what if I tell you that inside is not 255 at all? There is a number in the form with which your computer is more convenient to make calculations, and most likely it is in binary form: 0...0 1111 1111 .

That's just to bring you these bytes directly - a bad idea. First, it is a feature of the computer that you are currently using. Secondly, where and how to display? In the text box? They need a string consisting of characters. But you do not have them yet, only bare bytes. Displaying them directly is dangerous - in the used encoding of printable characters with such codes it may not be at all.

Therefore, when outputting to any text space, debuggers and many other means display values ​​in those representations in which these values ​​are clearer to you . For numbers, these are sequences of Arabic decimal digits. So historically, this format is understandable to almost the whole world.

Therefore, if you need to display a number in hexadecimal form, you will have to convert it to a string yourself , indicating the base of the number system .


Same with DateTime . Yes, he loses the format! And inside it keeps the date in such a crazy way that at first human sight it will not even be obvious that this is a moment in time. And it will be transformed into a string for output without other explicit instructions in the form that, according to the system prompt (locale), will be most understandable to you.

If you need to display the date in ISO8601, then (as above) you will have to explicitly convert it to a string indicating the format .

So it goes.