Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to calculate days hours minutes using minutes in ASP.NET

ex:28983 mins = 20 days 21 hrs
Posted
Updated 4-Jan-16 20:41pm
v2
Comments
[no name] 5-Jan-16 2:42am    
What you have tried so far? Could you please share your requirements clearly

You can use TimeSpan class as below code
C#
TimeSpan t = TimeSpan.FromSeconds( secs );

string answer = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", 
                t.Hours, 
                t.Minutes, 
                t.Seconds, 
                t.Milliseconds);
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 5-Jan-16 3:22am    
Shortest, anyways, you may want to convert the minutes to seconds first. :-)
Well, that's not difficult, considering a day corresponds to 60 * 24 = 1440 secs:

C#
int tot_mins = 28983;
int days = tot_mins / 1440;
int hours = (tot_mins % 1440)/60;
int mins = tot_mins % 60;


Please note, your example is wrong:
28983 total minutes -> 20 days, 3 hours, 3 minutes
 
Share this answer
 
v5
Comments
sekar305 5-Jan-16 6:11am    
worked thanks cpallini.....

but how manually calculate this line

int hours = (tot_mins % 1440)/60;
Try:
C#
// Use TimeSpan structure
int min = 28983;
TimeSpan elapsedTime = new TimeSpan( 0, min, 0 );

int day = elapsedTime.Days;
int hour = elapsedTime.Hours;

Console.WriteLine(string.Format("{0} days {1} hours", day, hour));

// or just plain mathematics

double days = min/60/24;
double hours = (min - days*24*60)/60;

Console.WriteLine(string.Format("{0} days {1:0} hours", days, hours));

Both get the same result as:
20 days 3 hours
20 days 3 hours
 
Share this answer
 

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