Declare variable for just time
I need to create a variable that contain开发者_如何学JAVAs time in the format hh:mm, how shall I do that?
DateTime currentTime = "00:00";
doesn't seem to do the trick. I need to add hours/minutes in a loop to that variable and keep the format "hh:mm". How is that done?
/M
Probably use TimeSpan
for that?
You should distinguish between the quantity you're trying to keep track of, and the eventual string formatting. They're different things.
Are you trying to maintain a time of day (in which case using DateTime
and ignoring the date part is probably best) or a duration of time (in which case TimeSpan
is most appropriate)? Either way, pick your data type, use it in your loop, and then deal with formatting as and when you need to.
(Just as a heads-up, I'm part of a new project called Noda Time which keeps all of these as different types; it's a port of the popular Joda Time project for Java. We're a very long way from releasing anything, but in a year's time I hope it would be the best answer for this question :)
Timespan is your best bet, or use DateTime.toString("hh:mm")
You could use the following and ignore the date part
DateTime current;
DateTime.TryParse("13:00", out current);
To get just the time out use
current.ToString("hh:mm");
You could use a normal datetime-variable with todays date, starting at midnight.
A simple integer? hours = number / 60; minutes = number % 60
精彩评论