gpt4 book ai didi

c# - 需要将字符串解析为 mm :ss not as hh:mm

转载 作者:行者123 更新时间:2023-12-04 02:28:52 25 4
gpt4 key购买 nike

我需要转换 string 值为 mm:ss(示例 - "6:12""59 :59", "6:6") 加起来就是hh:mm:ss格式的值。

目前我正在尝试做一件简单的事情,但向我抛出这个异常:

"System.FormatException: 'Input string was not in a correct format.'"

代码示例如下:

string time = "6:14";
TimeSpan ts = TimeSpan.ParseExact(time, "mm:ss", null);
string total = ts.ToString("hh:mm:ss");

最佳答案

您的代码有 2 个问题:

  1. 您使用自定义分隔符 : 应该使用\
  2. 转义
  3. 您不使用前导零 ("06:14"),这就是为什么您应该使用 m 而不是 mm(如果 “6:6” 是正确的值,则 s 而不是 ss):

代码:

  string time = "6:14";
TimeSpan ts = TimeSpan.ParseExact(time, @"m\:s", null);
string total = ts.ToString(@"hh\:mm\:ss");

关于c# - 需要将字符串解析为 mm :ss not as hh:mm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65541454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com