简体   繁体   English

使用NodaTime解析输入并输出不同的dateTime格式

[英]Using NodaTime to parse an input and output different dateTime formats

I am currently using NodaTime to parse dates and output dates 我目前正在使用NodaTime来解析日期和输出日期

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 this: 2014-03-11T02:00:00-07:00 例如,输入是: 2014-03-11T02:00:00-07:00

If my return statement is the following: return result.Value.ToString() , then the output would look like this: 2014-03-11T02:00:00-07 如果我的return语句如下: return result.Value.ToString() ,那么输出将如下所示: 2014-03-11T02:00:00-07

I understand the use of properties with NodaTime (which is a life saver), however, I am interested in outputs like this: 我理解使用NodaTime属性(这是一个救生员),但是,我对这样的输出感兴趣:

yyyy-MM-dd HH:mm:ss

yyyyMMdd HH:mm:ss

dd/MM/yyyy hh:mm

So I tried to change my return statement to this: 所以我试着把我的return语句更改为:

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

The final output of that format is: 3/11/2014 2:0 该格式的最终​​输出是: 3/11/2014 2:0

Is there anyway to craft the output so it's a fixed format like 03/11/2014 02:00 无论如何要制作输出,所以它是一个固定的格式,如03/11/2014 02:00

I know that if I input an 01 as my month, the output would be 1/11/2014 instead of 01/11/2014 我知道如果我输入01作为我的月份,输出将是1/11/2014而不是01/11/2014

您可以将格式发送到ToString方法:

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

As ANeves mentions, you can use the BCL-style formatting using ToString(pattern, IFormatProvider) , but it's also worth noting that all pattern objects already support a Format(value) method in addition to Parse(string) , so you could also write: 正如ANeves所提到的,你可以使用ToString(pattern, IFormatProvider)来使用BCL样式的格式,但是值得注意的是,除了Parse(string)之外,所有模式对象都已经支持Format(value)方法,所以你也可以写:

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

Which is perhaps more consistent with using patterns for parsing, and also happens to be more efficient if you can keep hold of the pattern and use it more than once. 这可能与使用模式进行解析更加一致,如果您可以保持模式并多次使用它,那么它也会更有效。

The Noda Time user guide has a section on Text handling that covers this in more detail. Noda Time用户指南有一个关于文本处理的部分,更详细地介绍了这一点。

Not directly related to your question, but: if you're formatting an OffsetDateTime for display without including the offset, you'd normally want to convert it to a LocalDateTime (perhaps with a fixed-offset time zone) to ensure that the results are comparable. 与您的问题没有直接关系,但是:如果您在不包含偏移量的情况下格式化OffsetDateTime以进行显示,则通常需要将其转换为LocalDateTime (可能具有固定偏移时区)以确保结果为可比。 As it stands, you'll return the same string for inputs of 2014-03-11T02:00:00-07:00 and also eg 2014-03-11T02:00:00-08:00 . 就目前而言,您将为2014-03-11T02:00:00-07:00以及2014-03-11T02:00:00-08:00输入返回相同的字符串。 Perhaps that's okay for your use case. 也许这对你的用例没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM