簡體   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