Get minutes count

  • Thread starter Thread starter David
  • Start date Start date
D

David

Hello.

I have a string with some time in it, for example "13:59".
How can I get how many minutes is in it?
Like this: "1:30" contains 90 minute.
Can anybody show me some example?

Tank you.
 
Thank you very much for helping.
Can you help me in one more situation...
How do I get time string from minutes?
Like: 90 convert to "01:30".

Catherine Lowery said:
David said:
Hello.

I have a string with some time in it, for example "13:59".
How can I get how many minutes is in it?
Like this: "1:30" contains 90 minute.
Can anybody show me some example?

Tank you.

You could try the following C# snipped:

private string GetMinutes(string s)
{
string[] temp = s.Split(':');
int minutes;

if (temp.Length == 2)
{
minutes = Int32.Parse(temp[0]);
minutes *= 60;
minutes += Int32.Parse(temp[1]);

return minutes.ToString();
}
else
return "Wrong format";
}

This code is far from ideal and works for time strings like "9:30" or
"1:00" so you will need to take measures to handle special cases like "2",
which could mean "two minutes" or "two hours". Hope this helps.

Regards

Catherine
 
If the format is always like that, then could simply parse on the colon and
convert each part to an integer. Then multiply the first one by 60 and add
the result to the second one.
 
David said:
Hello.

I have a string with some time in it, for example "13:59".
How can I get how many minutes is in it?
Like this: "1:30" contains 90 minute.
Can anybody show me some example?

Tank you.

You could try the following C# snipped:

private string GetMinutes(string s)
{
string[] temp = s.Split(':');
int minutes;

if (temp.Length == 2)
{
minutes = Int32.Parse(temp[0]);
minutes *= 60;
minutes += Int32.Parse(temp[1]);

return minutes.ToString();
}
else
return "Wrong format";
}

This code is far from ideal and works for time strings like "9:30" or
"1:00" so you will need to take measures to handle special cases like
"2", which could mean "two minutes" or "two hours". Hope this helps.

Regards

Catherine
 
David said:
I have a string with some time in it, for example "13:59".
How can I get how many minutes is in it?
Like this: "1:30" contains 90 minute.

Assuming your string always has hours and minutes, this will work:

string sTime = "1:30";
string[] sHoursMins = sTime.Split(new string[] { ':' });
int iHours = Convert.ToInt32(sHoursMins[0]);
int iMinutes = Convert.ToInt32(sHoursMins[1]);
int iTotalMinutes = iHours * 60 + iMinutes;

However, your code might be clearer if you use the TimeSpan class
instead.

P.
 
David... A good general approach is to look for an OO solution. If you
cannot find an OO solution in the framework, then consider writing your
own class. There is a class in the framework that encapsulates the
concept of time and has a built in method to return the number of
minutes.

TimeSpan ts= new TimeSpan(0,1,30,0);
System.Console.WriteLine(ts.TotalMinutes);
Output: 90

Regards,
Jeff
I have a string with some time in it, for example "13:59".
How can I get how many minutes is in it?
Like this: "1:30" contains 90 minute.
Can anybody show me some example?<
 
David said:
Thank you very much for helping.
Can you help me in one more situation...
How do I get time string from minutes?
Like: 90 convert to "01:30".

David wrote:

Hello.

I have a string with some time in it, for example "13:59".
How can I get how many minutes is in it?
Like this: "1:30" contains 90 minute.
Can anybody show me some example?

Tank you.

You could try the following C# snipped:

private string GetMinutes(string s)
{
string[] temp = s.Split(':');
int minutes;

if (temp.Length == 2)
{
minutes = Int32.Parse(temp[0]);
minutes *= 60;
minutes += Int32.Parse(temp[1]);

return minutes.ToString();
}
else
return "Wrong format";
}

This code is far from ideal and works for time strings like "9:30" or
"1:00" so you will need to take measures to handle special cases like "2",
which could mean "two minutes" or "two hours". Hope this helps.

Regards

Catherine
The following solution is one of the easiest imaginable an has some
drawbacks but works as expected:

private string GetTimeString(string s)
{
string result = String.Empty;
int minutes = Int32.Parse(s) % 60;
int hours = (int)Int32.Parse(s) / 60;
result = hours.ToString() + ":" + (minutes.ToString().Length == 1 ?
"0" + minutes.ToString() : minutes.ToString());
Console.WriteLine("Minutes: {0}", result);
return result;
}

To get the minutes I simply did a modulo and to obtain a value for the
hours I divided the same value by 60 and cut off the decimal part by
casting it to an int. Furthermore I fetched the length of the resulting
string to format it properly. If you pass a string, say "65", you will
get "1:05" instead of "1:5". This might not be the best solution but may
give you an idea how things work.

Regards

Catherine
 
Back
Top