c# TimeSpan Converting Minutes to Hours
Asked Answered
R

8

14

I am converting minutes into hours. So if I have minutes = 12534. The result should be 208:54. The below code fails to bring this result.

TimeSpan spWorkMin = TimeSpan.FromMinutes(12534);
string workHours = spWorkMin.ToString(@"hh\:mm");
Console.WriteLine(workHours);

The result is 16:54.

How to get it correct?

Resnatron answered 8/10, 2015 at 6:10 Comment(4)
time span convert into days to that's way you are not getting correct result.Imray
You should give more examples. What output do you expect for 1, 9, 10, 59, 60, 61 minutes? It's important for correct zero padding in the format, if needed.Yser
A complete test set would also include 1440, 6000, 594000, 594001 minutes.Yser
You are only displaying the hours:minutes part of the timespan. 12534 minutes is 8.70 days, your code is only converting fraction of a day (.70) to hh:mm.Hoick
A
16

The correct way to use is not using the ToString overload of DateTime – because there is no possibility to show the TotalHours there – but the string.Format method:

string.Format("{0:00}:{1:00}", (int)spWorkMin.TotalHours, spWorkMin.Minutes);
Albers answered 8/10, 2015 at 6:16 Comment(2)
TotalHours is a double, this would return "208.9:54" for the provided input.Henceforth
@GeorgePolevoy I have now changed the format to always display two digits.Albers
P
19
var totalMinutes = 12534;
Console.WriteLine("{0:00}:{1:00}", totalMinutes / 60, totalMinutes % 60);

Or

var totalMinutes = 12534;
var time = TimeSpan.FromMinutes(totalMinutes);
Console.WriteLine("{0:00}:{1:00}", (int)time.TotalHours, time.Minutes);

See https://dotnetfiddle.net/gYEsj2 to play with this

Plywood answered 8/10, 2015 at 6:18 Comment(3)
This would format incorrectly for number of minutes <10Yser
var totalMinutes = 1; // 0:1Yser
@GeorgePolevoy - Indeed. FixedPlywood
A
16

The correct way to use is not using the ToString overload of DateTime – because there is no possibility to show the TotalHours there – but the string.Format method:

string.Format("{0:00}:{1:00}", (int)spWorkMin.TotalHours, spWorkMin.Minutes);
Albers answered 8/10, 2015 at 6:16 Comment(2)
TotalHours is a double, this would return "208.9:54" for the provided input.Henceforth
@GeorgePolevoy I have now changed the format to always display two digits.Albers
A
4

You need use TimeSpan.TotalHours Property

The TotalHours property represents whole and fractional hours, whereas the Hours property represents whole hours.

    TimeSpan spWorkMin = TimeSpan.FromMinutes(12534);
    string workHours = spWorkMin.ToString(@"hh\:mm");
    Console.WriteLine(spWorkMin.TotalHours);

https://dotnetfiddle.net/JRCLra

Allmon answered 8/10, 2015 at 6:21 Comment(0)
D
1

The format specifier hh will show the hour part, which is not the total hours. You have to manually create a string using TotalHours cast into ints to show it as you want and add the minutes to that.

Drava answered 8/10, 2015 at 6:17 Comment(0)
H
0

From MSDN documentation:

The "hh" custom format specifier outputs the value of the TimeSpan::Hours property, which represents the number of whole hours in the time interval that is not counted as part of its day component.

One quick way of getting the result you want would be something like the following:

TimeSpan spWorkMin = TimeSpan.FromMinutes(12534);
string workHours = string.Format("{0}:{1:00}", (int)spWorkMin.TotalHours, spWorkMin.Minutes);
Console.WriteLine(workHours);
Henceforth answered 8/10, 2015 at 6:19 Comment(0)
A
0
TimeSpan spWorkMin = TimeSpan.FromMinutes(12534);
string workHours = string.Format("{0}:{1}", (int)spWorkMin.TotalHours, spWorkMin.Minutes);
Console.WriteLine(workHours);
Alyworth answered 8/10, 2015 at 6:20 Comment(0)
T
0

Personnaly I use this:

public static double MinutsTohHours(int Minuti)
    {
        double Tempo = 0;
            Tempo = ((double)(Minuti % 60) / 100);
            var n = (double)Minuti / 60;
            return Math.Floor((double)Minuti / 60) + Tempo;
       
    }
Thorton answered 29/12, 2021 at 20:10 Comment(0)
A
0
using System;

var days = 1; 
var hours = 23; //max 23
var min = 12; //max 59

var TotalMin = (days*24*60)+(hours*60)+min;

Console.WriteLine("TotalMins "+ TotalMin);

//return back to the original days,hours,minutes

var Days = (TotalMin/(24*60));
var _minutes = (TotalMin%(60*60));
var Hours = (_minutes/60);
var Minutes = _minutes - (Hours*60);

Console.WriteLine($"{Days} , {Hours} , {Minutes}");
Ambush answered 12/10, 2022 at 6:16 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.