TimeSpan.解析时间格式 hhmmss

发布于 2024-08-13 01:36:33 字数 145 浏览 2 评论 0原文

在 C# 中,我的时间格式为 hhmmss,例如 12:45:10 的 124510,我需要知道 TotalSeconds。我使用了 TimeSpan.Parse("12:45:10").ToTalSeconds 但它不采用 hhmmss 格式。有什么好的方法可以转换这个吗?

in c# i have time in format hhmmss like 124510 for 12:45:10 and i need to know the the TotalSeconds. i used the TimeSpan.Parse("12:45:10").ToTalSeconds but it does'nt take the format hhmmss. Any nice way to convert this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

风筝在阴天搁浅。 2024-08-20 01:36:33

这可能会有所帮助,

using System;
using System.Globalization;

namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime d = DateTime.ParseExact("124510", "hhmmss", CultureInfo.InvariantCulture);

            Console.WriteLine("Total Seconds: " + d.TimeOfDay.TotalSeconds);

            Console.ReadLine();
        }
    }
}

请注意,这不会处理 24 小时时间,要解析 24 小时格式的时间,您应该使用模式 HHmmss

This might help

using System;
using System.Globalization;

namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime d = DateTime.ParseExact("124510", "hhmmss", CultureInfo.InvariantCulture);

            Console.WriteLine("Total Seconds: " + d.TimeOfDay.TotalSeconds);

            Console.ReadLine();
        }
    }
}

Note this will not handle 24HR times, to parse times in 24HR format you should use the pattern HHmmss.

七婞 2024-08-20 01:36:33

将字符串解析为 DateTime 值,然后减去它的 Date 值以获得 TimeSpan 形式的时间:

DateTime t = DateTime.ParseExact("124510", "HHmmss", CultureInfo.InvariantCulture);
TimeSpan time = t - t.Date;

Parse the string to a DateTime value, then subtract it's Date value to get the time as a TimeSpan:

DateTime t = DateTime.ParseExact("124510", "HHmmss", CultureInfo.InvariantCulture);
TimeSpan time = t - t.Date;
雪化雨蝶 2024-08-20 01:36:33

您必须决定接收时间格式并将其转换为任何一致的格式。

然后,您可以使用以下代码:

格式:hh:mm:ss(12 小时格式)

DateTime dt = DateTime.ParseExact("10:45:10", "hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
double totalSeconds = dt.TimeOfDay.TotalSeconds;    // Output: 38170.0

格式:HH:mm:ss(24 小时格式)

DateTime dt = DateTime.ParseExact("22:45:10", "HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
double totalSeconds = dt.TimeOfDay.TotalSeconds;    // Output: 81910.0

如果是格式不匹配,将抛出 FormatException 并显示消息:“字符串未被识别为有效的日期时间。

You have to decide the receiving time format and convert it to any consistent format.

Then, you can use following code:

Format: hh:mm:ss (12 Hours Format)

DateTime dt = DateTime.ParseExact("10:45:10", "hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
double totalSeconds = dt.TimeOfDay.TotalSeconds;    // Output: 38170.0

Format: HH:mm:ss (24 Hours Format)

DateTime dt = DateTime.ParseExact("22:45:10", "HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
double totalSeconds = dt.TimeOfDay.TotalSeconds;    // Output: 81910.0

In case of format mismatch, FormatException will be thrown with message: "String was not recognized as a valid DateTime."

心舞飞扬 2024-08-20 01:36:33

您需要转义冒号(或其他分隔符),出于什么原因它无法处理它们,我不知道。请参阅 MSDN 上的 自定义 TimeSpan 格式字符串,以及接受的答案,来自Jon,为什么 TimeSpan.ParseExact 不起作用

You need to escape the colons (or other separators), for what reason it can't handle them, I don't know. See Custom TimeSpan Format Strings on MSDN, and the accepted answer, from Jon, to Why does TimeSpan.ParseExact not work.

笨死的猪 2024-08-20 01:36:33

如果您还想使用像“01:02:10.055”这样的格式的毫秒,那么您可以执行以下操作;

public static double ParseTheTime(string givenTime)
{
var time = DateTime.ParseExact(givenTime, "hh:mm:ss.fff", CultureInfo.InvariantCulture);
return time.TimeOfDay.TotalSeconds;
}

此代码将为您提供相应的秒数。
请注意,如果您想调整精度点,可以增加“f”的数量。

In case you want to work with also milliseconds like this format "01:02:10.055" then you may do as following;

public static double ParseTheTime(string givenTime)
{
var time = DateTime.ParseExact(givenTime, "hh:mm:ss.fff", CultureInfo.InvariantCulture);
return time.TimeOfDay.TotalSeconds;
}

This code will give you corresponding seconds.
Note that you may increase the number of 'f's if you want to adjust precision points.

笑看君怀她人 2024-08-20 01:36:33

如果您可以保证字符串始终为 hhmmss,您可以执行以下操作:

TimeSpan.Parse(
    timeString.SubString(0, 2) + ":" + 
    timeString.Substring(2, 2) + ":" + 
    timeString.Substring(4, 2)))

If you can guarantee that the string will always be hhmmss, you could do something like:

TimeSpan.Parse(
    timeString.SubString(0, 2) + ":" + 
    timeString.Substring(2, 2) + ":" + 
    timeString.Substring(4, 2)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文