15,565,146 members
See more:
How to convert minutes into d:hh:mm:ss in asp.net or sql
Posted

## Solution 1

Hi,

Just now I write this.You've to add some additional code for it to get perfect output.Here just 'm giving idea on your question.

C#
```int inp = int.Parse(TextBox1.Text);
int d = 0, h = 0, min = 0, se = 0;
d = inp / 1440;
inp = inp % 1440;
if (inp > 59)
{
h = inp / 60;
inp = inp % 60;
if (inp > 60)
{
min = inp / 60;
inp = inp % 60;
se = inp;
}
else
{
min = inp;
}

}
else
{
h = inp;
}
```

Try the above code for generating your requirement.In soon I'll post perfect code for it K

I hope u'll uderstand what I said.

v2
Milind Panchal 19-Sep-11 9:55am
Thanks

## Solution 3

Try this
C#
```int min = DateTime.Now.Minute;
TimeSpan output = new TimeSpan(0,min,0);
string str = output.ToString().Replace(".",":");
```

## Solution 4

for Sqlserver

SQL
```declare @min int
set @min = 1561
select @min / 1440  days,
(@min / 60) % 24 hours,
@min % 60 minutes```

## Solution 5

OP wanted also seconds in the result, so I assume he wants to give a non integer number as minutes (like 120,75 minutes)
This is what I'd do:

1. Take a decimal number representing Minutes (and fractions)
2. Convert to "Ticks" = 100 nano seconds
3. Initialize TimeSpan with calculated ticks.
4. Convert to desired output format (ToString)

Here is a little Testapp:

C#
```using System;

namespace MinutesToDateTime
{
class Program
{
static void Main(string[] args)
{
double dMinutes = 120.75d; // Minutes as decimal number
long iTicks = (long) (dMinutes * 60d * 10000000d); // Convert to Ticks
TimeSpan ts = new TimeSpan(iTicks); // Initialize TimeSpan with calculated Ticks

Console.WriteLine(ts.ToString("G")); // Convert To desired output format (see documentation for String.Format/ToString)

}
}
}```

Joris Janssens 19-Sep-11 17:51pm
TimeSpan has some convenient static methods to create an instance. TimeSpan.FromMinutes(double value) will eliminate the need for the conversion to ticks.
=> TimeSpan ts = TimeSpan.FromMinutes(120.75);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Dave Kreskowiak 75 OriginalGriff 60 Richard MacCutchan 35 merano99 30 George Swan 25
 OriginalGriff 200 Dave Kreskowiak 145 Richard MacCutchan 120 Graeme_Grant 70 k5054 65

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900