TimeSpan.Parse时间格式hhmmss [英] TimeSpan.Parse time format hhmmss

查看:353
本文介绍了TimeSpan.Parse时间格式hhmmss的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,我有格式为hhmmss的时间,例如12510: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?

推荐答案

这可能会帮助

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();
        }
    }
}

请注意,这不会处理24HR时间,以24HR格式解析时间,您应该使用 HHmmss 模式。

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

这篇关于TimeSpan.Parse时间格式hhmmss的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆