如何从结构HH:MM得到分钟 [英] How get Minutes from structure HH:MM

查看:75
本文介绍了如何从结构HH:MM得到分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个结构{HH:MM}

示例:

  string  duracion =   26:30; 
TimeSpan duraciontime = TimeSpan.Parse(duracion);
int result = Convert.ToInt32(duraciontime.TotalMinutes);



如果小时数< 24我没有问题,但如果时间> 24不起作用。

目前我正在使用TryParse进行测试。
$ b $b¿更多解决方案?

非常感谢。

解决方案

试试这个:

  string  duration =   26:30; 
string [] a = duration.Split(' );
if (a.length > = 2 ){
int result = int .Parse(a [ 0 ])* 60 + int .Parse(a [ 1 < /跨度>]);
}


我以前使用这种方法处理过这个问题:

 私人 TimeSpan TimeSpanEx( int 天=  0  int  hours =  0  int  minutes =  0  int  seconds =  0  int  milliseconds =  0 
{
if (毫秒> 999
{
秒+ =毫秒/ 1000;
毫秒=毫秒%1000;
}

如果(秒> 59
{
分钟+ =秒/ 60 ;
秒=秒% 60 ;
}

如果(分钟> 59
{
小时+ =分钟/ 60 ;
分钟=分钟% 60 ;
}

如果(小时> 23
{
days + = hours / 24 ;
小时=小时% 24 ;
}

返回 TimeSpan(天,小时,分钟) ,秒,毫秒);
}

所以,如果我想要一个表示25小时和125分钟的TimeSpan对象:

 TimeSpan tSpan = TimeSpanEx(小时: 25 ,分钟: 125 ); 

> ? tSpan
{ 1 03 05 00 }
天数: 1
小时: 3
毫秒: 0
分钟数: 5
秒: 0
Ticks: 975000000000
TotalDays: 1 1284722222222221
TotalHours: 27 083333333333332
TotalMilliseconds: 97500000 0
TotalMinutes: 1625 0
TotalSeconds: 97500 0


你正在使用Parse(),而不是TryParse()。错误是因为.NET中没有第26个小时(也是在我们这个时代)。所以当你通过26:30它会抛出错误。但是你可以在一分钟内传球26分,比如这个00:26:30。



这个 [ ^来自MSDN的文档包含您可以传递的TimeSpan的不同字符串表示的示例。我也可以在这里写一下。



  string  [] values = {  6  6:12  6:12:14  6:12:14:45
6.12:14:45 6:12:14:45.3448
6: 12:14:45,3448 6:34:14:45 };

// 当前文化:en-US
// 6 - > 6.00:00:00
// 6:12 - > 06:12:00
// 6:12:14 - > 06:12:14
// 6:12:14:45 - > 6.12:14:45
// 6.12:14:45 - > 6.12:14:45
// 6:12:14:45.3448 - > 6.12:14:45.3448000
// 6:12:14:45,3448:不好格式
// 6:34:14:45:溢出





所以,你看,通过26:30将导致.NET认为你已经过了26个小时。我为你测试了它创建了一个dotnetfiddle,
这里 [ ^ ]它是。你可以在控制台中看到第三个结果(下图)它有第26分钟的东西。在第三个例子中,分钟数为26.因此在代码中使用它们将返回26分钟到您的应用程序。


I have this structure {HH:MM}
Example:

string duracion = "26:30";
TimeSpan duraciontime = TimeSpan.Parse(duracion);
int result = Convert.ToInt32(duraciontime.TotalMinutes);


If the hours < 24 I haven't got problem but if the hours > 24 doesn't work.
At the moment I am testing with TryParse.
¿More solutions?
Thanks so much.

解决方案

Try this :

string duration = "26:30";
string[] a = duration.Split(':');
if (a.length >= 2) {
int result = int.Parse(a[0])*60 + int.Parse(a[1]);
}


I have handled this in the past by using this method:

private TimeSpan TimeSpanEx(int days = 0, int hours = 0, int minutes = 0, int seconds = 0, int milliseconds = 0)
{
    if (milliseconds > 999)
    {
        seconds += milliseconds/1000;
        milliseconds = milliseconds%1000;
    }

    if (seconds > 59)
    {
        minutes += seconds / 60;
        seconds = seconds % 60;
    }

    if (minutes > 59)
    {
        hours += minutes / 60;
        minutes = minutes % 60;
    }

    if (hours > 23)
    {
        days += hours / 24;
        hours = hours % 24;
    }

    return new TimeSpan(days, hours, minutes, seconds, milliseconds);
}

So, if I wanted a TimeSpan object that expressed 25 hours, and 125 minutes:

TimeSpan tSpan = TimeSpanEx(hours: 25, minutes:125);

> ? tSpan
{1.03:05:00}
    Days: 1
    Hours: 3
    Milliseconds: 0
    Minutes: 5
    Seconds: 0
    Ticks: 975000000000
    TotalDays: 1.1284722222222221
    TotalHours: 27.083333333333332
    TotalMilliseconds: 97500000.0
    TotalMinutes: 1625.0
    TotalSeconds: 97500.0


You're using Parse(), not TryParse(). And the error is because there is no 26th hour in .NET (also in our time). So when you pass 26:30 it throws error. You can however pass 26 as a minute, like this "00:26:30".

This[^] documentation from MSDN has the examples of different string representations of the TimeSpan that you can pass. Let me write them here too.

string[] values = { "6", "6:12", "6:12:14", "6:12:14:45", 
                    "6.12:14:45", "6:12:14:45.3448", 
                    "6:12:14:45,3448", "6:34:14:45" };

//    Current Culture: en-US 
//    6 --> 6.00:00:00 
//    6:12 --> 06:12:00 
//    6:12:14 --> 06:12:14 
//    6:12:14:45 --> 6.12:14:45 
//    6.12:14:45 --> 6.12:14:45 
//    6:12:14:45.3448 --> 6.12:14:45.3448000 
//    6:12:14:45,3448: Bad Format 
//    6:34:14:45: Overflow



So, you see, passing "26:30" would cause the .NET to think you're passing a 26th hour to it. I have created a dotnetfiddle for you test it, here[^] it is. You can see the third result in the console (bottom) it has the 26th minute kind of thing. In the third instance the minutes are 26. So using them in your code would return 26 minutes to your application.


这篇关于如何从结构HH:MM得到分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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