tags:

views:

1895

answers:

2

Hi!!

How can I convert an int 90, for example, to DateTime 1:30 in C# 3.0?

Thanks!!

+22  A: 

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.

Erik Forbes
But, I really need that transformed to DateTime, otherwise will throw an error!
That's a shame - ah well. Use my second suggestion, then, and you should be good to go.
Erik Forbes
+1  A: 

Or if you're trying to add time to a DateTime with just a date:

dateTime.AddMinutes(90);
James L
DateTime is immutable - calling AddMinutes() will return a new DateTime with the addition performed. Ignoring the return value will result in the appearance that nothing happened.
Erik Forbes