English 中文(简体)
我如何在HHH:MM:SS格式时间说明中进行区分?
原标题:How do I do division on HH:MM:SS format time strings in C#?

我有过一系列时间来到我,作为网络服务机构的载体。 时间为HHHH:MM:SS:000(3秒)。 我需要作两次比较,以确定一个人是否比另一方高出一倍多:

if ( timeA / timeB > 2 )

什么最简单的处理时间问题?


If I was writing in Python this would be the answer to my question: Difference between two time intervals?

(除操作者I需要的是分行,而不是分机)


Edit:我真心想的是,获得时间A到时间B的比例,需要分治而不是分治。 不幸的是,日间结构似乎没有司级操作员。 更新问题标题和内容,以反映这一点。


<>Solution:

根据我下面的回答,这是迄今为止所有拟议方法中最简单的办法,这里是:

DateTime timeA;
DateTime timeB;
DateTime.TryParse(webServiceTimeString_A, out timeA);
DateTime.TryParse(webServiceTimeString_B, out timeB);

// TimeA is more than twice the duration of TimeB.
if ( (double)timeA.TimeOfDay.Ticks / (double)timeB.TimeOfDay.Ticks > 2.0f )
{
    // Do stuff.
}
else
{
    // Do different stuff.
}

http://www.ohchr.org。

最近,在贾瓦文中,还需要这一功能,才能发出美国宇宙航空研究开发厅的号召,因此,我不得不在一切之后写起转换功能(C#中不作调整)。 必要时:

if (_timeInSeconds(timeA) / _timeInSeconds(timeB) > 2) {
    // Do stuff.
}

// Convert HH:MM:SS:000 string to a number of seconds so we can do math on it.
function _timeInSeconds(stringTime) {
    var timeArray = stringTime.split(":");
    var timeInSeconds = 0;

    //HH
    timeInSeconds += (parseInt(timeArray[0], 10) * 3600);

    //MM
    timeInSeconds += (parseInt(timeArray[1], 10) * 60);

    //SS
    timeInSeconds += (parseInt(timeArray[2], 10));

    //Milliseconds
    timeInSeconds += (parseInt(timeArray[3], 10) / 1000);

    return timeInSeconds;
}

致明智之辞: 确保明确“平等”的第二个论点。

parseInt(string, 10)

......具体指出,该指示是基地10号。 否则,如果扼杀从0(common in HH:MM:SS form)开始,Java 则将其定为“基地8”号。 由此可见<代码>08><>>>>>和09>,改成正文编码<0<>>(因为8和9在基地-8中不存在),计算结果被撤销。

最佳回答

>timeSpan structure,然后 rel=“nofollow”>。 计算时间与.NET

实际上,你的法典可以简化:

    DateTime timeA = DateTime.Now;
    DateTime timeB = DateTime.Now.AddHours(-10.0);

    if ( (double)timeA.TimeOfDay.Ticks / (double)timeB.TimeOfDay.Ticks > 2.0f )
        Console.WriteLine("Time A is more than twice time B");
    else
        Console.WriteLine("Time A is NOT more than twice time B");
问题回答

首先请在parsing the string,然后上建立。 数学易于:

请注意,在<代码>-上减去两个日期后,操作员将返回>> /timeSpan,对MSDN docs进行类似检查。

I think the easiest way to parse the strings is with TimeSpan.ParseExact in .Net 4:

    public static bool MoreThanDouble(string t1, string t2)
    {
        const string format = @"%h:mm:ss:fff";
        long ticks1 = TimeSpan.ParseExact(t1, format, null).Ticks,
             ticks2 = TimeSpan.ParseExact(t2, format, null).Ticks;
        return ticks1 - ticks2 > ticks2;
    }

    static void Main(string[] args)
    {
        Console.WriteLine(MoreThanDouble("10:11:12:123", "1:23:45:000"));
        Console.WriteLine(MoreThanDouble("10:11:12:123", "9:23:45:000"));
    }

That will print True False. If you don t have .Net 4, you can use DateTime:

    public static bool MoreThanDouble2(string t1, string t2)
    {
        const string format = @"%h:mm:ss:fff";
        long ticks1 = DateTime.ParseExact(t1, format, null,
             System.Globalization.DateTimeStyles.NoCurrentDateDefault).Ticks,
             ticks2 = DateTime.ParseExact(t2, format, null,
             System.Globalization.DateTimeStyles.NoCurrentDateDefault).Ticks;
        return ticks1 - ticks2 > ticks2;
    }

使用<代码>Datetime.FormatExact()从你的身边转换到日期,然后通过不同方式使您获得一个<>timeSpan。

我只想把插头划入一个时段,然后改成一个<条码>。

Here s a sample of how you could do this:

class Program
{
    static void Main( string[] args )
    {
        Console.WriteLine( ( "02:00:00:001".ToTimeSpan().TotalMilliseconds / "01:00:00:000".ToTimeSpan().TotalMilliseconds ) > 2 );
        Console.WriteLine( ( "02:00:00:001".ToTimeSpan().TotalMilliseconds / "00:60:00:000".ToTimeSpan().TotalMilliseconds ) > 2 );
        Console.WriteLine( ( "02:00:00:000".ToTimeSpan().TotalMilliseconds / "01:00:00:001".ToTimeSpan().TotalMilliseconds ) > 2 );
        Console.WriteLine( ( "25:12:60:002".ToTimeSpan().TotalMilliseconds / "12:12:60:002".ToTimeSpan().TotalMilliseconds ) > 2 );
    }
}

public static class Helpers
{
    public static TimeSpan ToTimeSpan(this string time )
    {
        var split = time.Split(  :  );
        if( split.Length != 4 )
        {
            throw new InvalidOperationException("Invalid format");
        }
        //First posistion is days.
        return new TimeSpan(0, split[ 0 ].ToInt(), split[ 1 ].ToInt(), split[ 2 ].ToInt(), split[ 3 ].ToInt() );
    }

    public static int ToInt( this string str )
    {
        return Convert.ToInt32( str );
    }
}

你可以根据你能期望时间的长短,对上述法典作一些必要的补充。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签