Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all
i have variable holds numbers of hours like "3" and its minutes in another variable "45" , i need to make anew variable holds time as a whole without date which contains hour + its minutes "03:45" ,

any help pleaseeeeee
thanks
Posted
Updated 24-Sep-12 23:04pm
v2
Comments
Zoltán Zörgő 25-Sep-12 5:14am    
But what kind of variable? A DateTime can hold it - it depend on you if you want to use the date part or not; an integer can hold the number of minutes or seconds, a float can hold the number of hours (like the days in Excel)...

Use a TimeSpan structure.

int hours = 3, minutes = 45, seconds = 0;
C#
TimeSpan myTime = new TimeSpan(hours, minutes, seconds);
 
Share this answer
 
v2
Comments
Umer Aziz Malik 25-Sep-12 5:18am    
My 5. Perfect answer imo.
Oshtri Deka 25-Sep-12 6:28am    
Thank You!
Ganesh Nikam 25-Sep-12 6:50am    
5+
Oshtri Deka 26-Sep-12 2:43am    
Thank you.
So, what about this code:
C#
int nHours = 3;
int nMinutes = 45;
int nTotalMinutes = nHours * 60 + nMinutes;

MessageBox.Show("Time: " + (nTotalMinutes / 60).ToString() + ":" + (nTotalMinutes % 60).ToString()); //Time: 3:45
 
Share this answer
 
v2

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