将分钟转换为全职 C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8819197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:37:40  来源:igfitidea点击:

Convert minutes to full time C#

c#datetimetime

提问by soamazing

I need convert 1815 minutesto 30:15(30 hours and 15 minutes)

我需要将1815 分钟转换为30:1530 小时 15 分钟

Is there an easy way to do this that I am missing?

有没有一种简单的方法可以做到这一点,我错过了?

采纳答案by Daniel Hilgarth

Use TimeSpan.FromMinutes:

使用TimeSpan.FromMinutes

var result = TimeSpan.FromMinutes(1815);

This will give you an object that you can use in different ways.
For example:

这将为您提供一个可以以不同方式使用的对象。
例如:

var hours = (int)result.TotalHours;
var minutes = result.Minutes;

回答by Amar Palsapure

Try TimeSpan.FromMinutes(minutes), this will give you TimeSpan, after that you can check TimeSpan.Hoursand TimeSpan.Minutesproperties.

试试TimeSpan.FromMinutes(minutes),这会给你TimeSpan,之后你可以检查TimeSpan.HoursTimeSpan.Minutes属性。

回答by Dwayne Hinterlang

        DateTime d = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
        Console.WriteLine(d.ToLongTimeString());
        Console.WriteLine(d.AddMinutes(1815).ToLongTimeString());
        Console.ReadLine();

回答by Giorgio Minardi

you can use this function

你可以使用这个功能


//minutes to be converted (70minutes = 1:10 hours)
int totalminutes = 70;
//total hours
int hours = 70 / 60;
//total minutes
int minutes = 70 % 60;
//output is 1:10
var time = string.Format("{0} : {1}", hours, minutes);

回答by Adnan Aziz

You can use this function to get the desired string

您可以使用此函数来获取所需的字符串

  public string GetTimeString(int durationInMinute)
  {
            TimeSpan timeSpan = TimeSpan.FromMinutes(durationInMinute);

            if(timeSpan.Hours == 1 && timeSpan.Minutes == 1)
                return timeSpan.Hours + " Hour and " + timeSpan.Minutes + " Min";
            else if (timeSpan.Hours > 1 && timeSpan.Minutes > 1)
                return timeSpan.Hours + " Hours and " + timeSpan.Minutes + " Mins";
            else if (timeSpan.Hours > 1 && timeSpan.Minutes < 1)
                return timeSpan.Hours + " Hours";
            else if(timeSpan.Hours < 1 && timeSpan.Minutes > 1)
                return timeSpan.Minutes + " Mins";
            else if(timeSpan.Hours == 1 && timeSpan.Minutes > 1)
                return timeSpan.Hours + " Hour and " + timeSpan.Minutes + " Mins";
            else if (timeSpan.Hours == 1 && timeSpan.Minutes == 0)
                return timeSpan.Hours + " Hour";
            else if (timeSpan.Hours == 0 && timeSpan.Minutes == 1)
                return timeSpan.Minutes + " Min";
            else
                return timeSpan.Hours + " Hours and " + timeSpan.Minutes + " Mins";
}

回答by Hamit YILDIRIM

In razor this line resolved my same situation

在剃刀这条线解决了我同样的情况

@TimeSpan.FromMinutes(@landing.Duration).Hours @R.Hour @TimeSpan.FromMinutes(@landing.Duration).Minutes @R.Minute

The Output with the resource file has given the formatted and cultural result like below

带有资源文件的输出给出了如下所示的格式和文化结果

2 h 55 m => english

2 sa 55 dk => turkish

2 ora 55 min => italian

2 小时 55 米 => 英语

2 sa 55 dk => 土耳其语

2 或 55 分钟 => 意大利语