繁体   English   中英

使用DateTime.TryParseExact时,是否可以忽略日期时间字符串的一部分?

[英]is there a way to ignore part of a datetime string when using DateTime.TryParseExact?

我的日期时间字符串看起来像这样

19430403000000-0400

但是我只关心破折号之前的值(我知道它的UTC偏移量,但是我不需要该偏移量值)

所以我想尽可能使用DateTime.TryParseExact,因为我有一个已经实现了它的类,其中用户将datetime格式作为字符串传递

所以我想要这样的东西

"yyyyMMddhhmmss-####"

用户可以忽略部分字符串格式

那可能吗?

我想我仍然必须指定整个格式,我只需要使用DateTimeOffset而不是DateTime。 似乎可以解析它了。

string date8 = "19430403000000-0400";

        DateTimeOffset result2;
        bool parsed = DateTimeOffset.TryParseExact(date8, "yyyyMMddhhmmss zzzz", CultureInfo.InvariantCulture,
                               DateTimeStyles.AllowWhiteSpaces,
                               out result2);
string str = "19430403000000-0400";
string output = str.Split("-")[0];

暂无
暂无

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

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