tags:

views:

673

answers:

5

I have a minute value and i want to have to 2 string values one with how many hours and the other with the minutes.

e.g.

Value - 121 minutes

string hours = 2
string minutes = 1

Value - 58 minutes

string hours = 0
string minutes = 58

How can I work this out in C#?

+1  A: 
int value = 121;
int hours = value / 60; // 2
int minutes = value % 60; // 1

string strHours = hours.ToString();
string strMinutes = minutes.ToString();
abatishchev
Using TimeSpan is the OO way, but this is short and sweet.
spoulson
I looked into TimeSpan using Reflector: all results are counted by multiplication of ticked milliseconds. I believe my method can be faster!
abatishchev
+8  A: 

Use a Timespan struct and its Parse method.

http://msdn.microsoft.com/en-us/library/system.timespan.parse.aspx

Jason Diller
He said he had a minute and wanted to convert it to strings, not the other way round.
froh42
Timespan is still the way to go.
Joel Coehoorn
+1  A: 
int value = 121;
int hours = value / 60;
int minutes = value % 60;
Dead account
Why the downvote? This is a perfectly valid way of getting hours/minutes from a total minutes value and is the same as abatishchev's answer except for the .ToString()'s
CraigTP
+1 to counter the -1
Davy8
I didn't -1 anyone :)
Dead account
@Craig: I screwed. There was an error in another answer and downvoted that one by mistake. I removed both -1.
Coincoin
+23  A: 
var span = System.TimeSpan.FromMinutes(121);
var hours = ((int)span.TotalHours).ToString();     
var minutes = span.Minutes.ToString();

The ToString() is because you asked for string values ...

TotalHours are the complete hours in the TimeSpan, they can be more than 24 (whereas the "Hours" field has a maximum of 24)

Oh, and on second thought: Why use the TimeSpan and not calculate it yourself? Because TimeSpan is already there debugged & tested by Microsoft, it has a nice clean interface (looking at the code you easily see whats going on without having to follow a calculation mentally) and it easily extends to further solutions. (Have the input in seconds? Use TimeSpan.FromSeconds(). Want the days? Use span.TotalDays ...)

Update:

I just noticed mistake in my answer: TotalHours returns a fractional value of all the hours, so we have to truncate it to an integer before converting it to a string.

froh42
This won't pad minutes with zeros. Adding ToString("00") should fix it.
Coincoin
@Coincoin: look at OP's example for 121 minutes. No padding is required.
Renze de Waal
My mistake. Thanks for pointing it out.
Coincoin
A: 
int value = 121;
TimeSpan timeSpan = TimeSpan.FromMinutes(value);

// gives you the rounded down value of 2
int hours = timeSpan.Hours; 

// gives you the minutes left of the hour
int minutes = value - (hours * 60);
eduncan911
Why not use TimeSpan.Minutes here? This will return 1 in this case, as opposed to TimeSpan.TotalMinutes which returns 121
Patrick McDonald
Actually, TotalMinutes returns a double that could return 121.3523 or some number if you are doing a difference in date. Which, yes, would be casted down to 121 if you are casting to an Int32.TimeSpam.Minutes I did not test. But, if it returns "1" instead of "121", then that's it.
eduncan911