Using NodaTime to parse input and output of different dateTime formats

I am currently using NodaTime to parse output dates and dates

public static string nodaTimeTest6(string input)
{
    var defaultValue = new OffsetDateTime(new LocalDateTime(2000, 1, 1, 0, 0), Offset.Zero);
    var pattern = OffsetDateTimePattern.Create("yyyy-MM-dd'T'HH:mm:sso<m>", CultureInfo.InvariantCulture, defaultValue);
    var result = pattern.Parse(input);

    return result.Value.Month + "/" + result.Value.Day + "/" + result.Value.Year + " " + result.Value.ClockHourOfHalfDay;
}

The input, for example, is: 2014-03-11T02:00:00-07:00

If my statement returnlooks like this: return result.Value.ToString()then the output will look like this:2014-03-11T02:00:00-07

I understand the use of properties with NodaTime (which is a life saver), however, I am interested in the following results:

yyyy-MM-dd HH:mm:ss

yyyyMMdd HH:mm:ss

dd/MM/yyyy hh:mm

So, I tried changing my statement returnto this:

return result.Value.Month + "/" + result.Value.Day + "/" + result.Value.Year + " " + result.Value.Hour + ":" + result.Value.Minute;

The end result of this format is: 3/11/2014 2:0

In any case, to process the output so that a fixed format, such as 03/11/2014 02:00

I know that if I enter 01as my month, the output will be 1/11/2014instead01/11/2014

+4
2

ToString:

return result.Value.ToString("dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture);
+5

ANeves, BCL ToString(pattern, IFormatProvider), , Format(value) Parse(string), :

var displayPattern = OffsetDateTimePattern.Create("MM/dd/yyyy' 'HH:mm", CultureInfo.InvariantCulture, defaultValue);
return displayPattern.Format(result.Value);

, , , .

Noda Time , .

, : OffsetDateTime , LocalDateTime (, ) . 2014-03-11T02:00:00-07:00, , , 2014-03-11T02:00:00-08:00. , .

+2

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


All Articles