Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to convert minutes into d:hh:mm:ss in asp.net or sql
Posted

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.
 
Share this answer
 
v2
Comments
Milind Panchal 19-Sep-11 9:55am    
Thanks
Try this
C#
int min = DateTime.Now.Minute;
TimeSpan output = new TimeSpan(0,min,0);
string str = output.ToString().Replace(".",":");
 
Share this answer
 
for Sqlserver

SQL
declare @min int
set @min = 1561
select @min / 1440  days,
(@min / 60) % 24 hours, 
@min % 60 minutes
 
Share this answer
 
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)

            Console.ReadKey();
        }
    }
}
 
Share this answer
 
Comments
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)



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