Reputation: 771
I have a list of several DateTimes. The DateTime is actually a String in HH:mm:ss format which i convert to DateTime.
From this list i retrieve the largest value (in minutes)
I calculate the total Minutes like the following:
//t is a DateTime
Double totalMin = TimeSpan.Parse(t.ToString()).TotalMinutes;
The largest value (in minutes) should be used as the "100%" value. I already have calculated the largest value of the list.
Meaning all the other values (also in minutes) in the list will be calculate based on this value. The other lower values have to scale on this max value in other words.
This will be used in a bar chart. So the largest value will take the most space followed by the lower values.
How exactly can i do this?
Thanks PeterP.
Upvotes: 0
Views: 1294
Reputation: 419
A Timespan is the difference, in some unit of time, between two DateTime's. You're parsing a DateTime. You're gonna get a FormatException:
Double UhOh = TimeSpan.Parse(DateTime.Now.ToString()).TotalMinutes;
results in:
System.FormatException: "Input string was not in a correct format."
Now to answer your question, you're going to need another piece of data: Another DateTime in order to find the difference, in minutes, between those two points.
Let's assume you want to use:
DateTime.Now
This would work:
DateTime t = new DateTime(2012,4,19,0,0,0);
TimeSpan ts = DateTime.Now - t;
Double mins = ts.TotalMinutes;
Hope that helps. (:
Upvotes: 1
Reputation: 38210
You are parsing Timespan
and converting DateTime
to a string which is not really required.
Do you mean something like this
List<DateTime> lstdt = new List<DateTime>() {
new DateTime(2010,5,5,1,10,0),
new DateTime(2011,5,5,2,10,0),
new DateTime(2010,8,5,1,10,0),
new DateTime(2010,5,5,1,10,0),
new DateTime(2011,11,5,1,10,0),
new DateTime(2010,12,5,1,10,0),
};
double maxval = lstdt.Max(c => c.TimeOfDay.TotalMinutes);
List<double> lstpercent = lstdt.Select(d1 => d1.TimeOfDay.TotalMinutes/maxval *100.00).ToList<double>();
Upvotes: 0
Reputation: 17631
You want to know how to calculate a percentage? Divide the current by the max and multiply it with 100. Then you can format the percentage like you want (like "20%", "20.2%", ...).
Double max = 200d;
Double current = 23d;
Double percent = current / max * 100;
Upvotes: 3