How can I convert an int 90, for example, to DateTime 1:30 in C# 3.0?
Thanks!!
Best Answer
You shouldn't use a DateTime to represent a span of time - use TimeSpan for that. And in such a case, you'd use this:
TimeSpan ts = TimeSpan.FromMinutes(90);
If you insist that you need a DateTime, you could do the following:
DateTime dt = DateTime.Now.Date; // To get Midnight Today
dt = dt.AddMinutes(90); // to get 90-minutes past midnight Today.
The reason you probably don't want to use DateTime, though, is that it (aptly named) combines the concept of Date with the concept of Time. Your question suggests that you're planning to ignore the date component, so in the interests of using the right tool for the job, I suggest TimeSpan.
// Save today's date.
var today = DateTime.Today;
// Calculate the age.
var age = today.Year - birthdate.Year;
// Go back to the year in which the person was born in case of a leap year
if (birthdate.Date > today.AddYears(-age)) age--;
However, this assumes you are looking for the western idea of the age and not using East Asian reckoning.
Best Answer
You shouldn't use a DateTime to represent a span of time - use TimeSpan for that. And in such a case, you'd use this:
If you insist that you need a DateTime, you could do the following:
The reason you probably don't want to use DateTime, though, is that it (aptly named) combines the concept of Date with the concept of Time. Your question suggests that you're planning to ignore the date component, so in the interests of using the right tool for the job, I suggest TimeSpan.