tags:

views:

60

answers:

4

I want to calculate the quarter Hour from the given two datetimes:

Suppose datetime1 and datetime2 , i am doing somewhat like this :

int d = datetime2.Subtract(datetime1).Hours;

 double l = Math.Round(Convert.ToDouble(d/4));

but if the QuarterHour is 3 (ie d=3), whats the good way to show the output- should it be zero or the round one : 3/4 = 0.75. so should i round up to 1 or 0 is ok. And if round up to 1..how should i?

+1  A: 

If you want to know the difference between the two date/times in fractions of an hour (e.g. 0.75) then this will do it:

DateTime a = new DateTime(2010, 05, 12, 13, 00, 00);
DateTime b = new DateTime(2010, 05, 12, 13, 45, 00);
double difference = b.Subtract(a).Minutes / 60.0;

Here, difference = 0.75

Daniel Renshaw
A: 

Not sure what you are asking here. Your code calculates the number of hours between the two date times, divides it by 4 then rounds it. It sems pretty strange thing to want to do, but whether you should be doing it or not depends on why you are doing it, we have no context at the moment.

Ben Robinson
A: 

After picking up ccornet answer, you can do it in a fancy four liner:

var elapsedTime = DateTime.Now - DateTime.Today;
var quarterlyMinutes = (Math.Round(elapsedTime.TotalMinutes / 15) / 4) * 60;
var quartelySpan = new TimeSpan(0, (int)quarterlyMinutes, 0);
var output = quartelySpan.ToString();

By this solution you'll get the nearest quarterly hour reached. This means at 11:23 this will round up to 11:30 and 11:22 will round down to 11:15.

If you prefer to get only already fullfilled quarterly hours, you can instead use:

var quarterlyMinutes = (Math.Floor(elapsedTime.TotalMinutes / 15) / 4) * 60;

This will lead to 11:15 also if the TimeSpan already shows 11:29 minutes are passed.

Last but not least to get every started quarterly hour you can take

var quarterlyMinutes = (Math.Ceiling(elapsedTime.TotalMinutes / 15) / 4) * 60;

and you'll get every started quarter. So for 11:01 this will lead to 11:15.

Oliver
+1  A: 

Since getting "DateTime.Hours" won't get you the quarter hours on its own, it's not too useful in that respect. "DateTime.TotalHours" almost works, but since a quarter hour is just 15 minutes, we can simplify from dealing with needlessly complex decimal-hours just by using this.

double l = datetime2.Subtract(datetime1).TotalMinutes / 15.0

You can surround this with Math.Round or any similar method to get a round number of quarter hours. Use whatever method fits your preferred rounding style (such as flooring or ceiling...ing). I would recommend Math.Round, myself.

double l = Math.Round(datetime2.Subtract(datetime1).TotalMinutes / 15.0)

And if you want to express this as the total hours with fractions only being quarter hours, you can just divide the rounded result by 4.

double l = Math.Round(datetime2.Subtract(datetime1).TotalMinutes / 15.0) / 4

Now, suppose instead of showing hours in decimal, we wanted to show the quarter-hour difference between two date times as a time-like value. For example, render 11 quarter hours as "2:45" instead of "2.75". This naturally won't work for storing as an actual number, but if you need to output you'd probably build the string like this.

double l = Math.Round(datetime2.Subtract(datetime1).TotalMinutes / 15.0) / 4
double dLeft = Math.Floor(l);
double dRight = (l - dLeft) * 60.0;
string output = dLeft.ToString() + ":" + dRight.ToString();
ccomet