C #: How to convert string to date according to donor format?

I have 2 lines: one date value, for example "20101127", the second - the format "yyyymmdd". How can I extract a date from a value using this format?

thank

+3
source share
3 answers

Use DateTime.ParseExact:

DateTime time = DateTime.ParseExact("20101127", "yyyyMMdd", null);

nullwill use the current culture, which is somewhat dangerous. You can also specify a specific culture, for example:

DateTime time = DateTime.ParseExact("20101127", "yyyyMMdd", CultureInfo.InvariantCulture);
+5
source

Use DateTime.ParseExact(). Please note that month MM, not MM.

var dateValue = DateTime.ParseExact("20101127", "yyyyMMdd",
    CultureInfo.InvariantCulture);
+2
source

Use the method ParseExact.

+1
source

Source: https://habr.com/ru/post/1779263/


All Articles