C # DateTime: double time scaling

I have a function that returns step size from a limited range. Therefore, if the range is {1,2,3,4,5,6, .10}, and I want 5 steps, it will return the size of step 2. This is not so difficult.

If I have {.1, .2, .3, .4 .... 1} and I want 7 steps, the step size is 0.14285. Then I want to convert 0.14285 to the nearest relevant time dimension. In this case, .14285 is a fractional day. For example, the integer 1 would represent the whole day, and .25 would represent 6 hours.

.14285 = 12,342.24 seconds = 205.704 Minutes = 3.42 Hours ~= 4 hours.

Then I want to bind .14285 to (4 * 60 * 60 = 14,400 / (24 * 60 * 60)) = .16666, which is the decimal equivalent in 4 hours.

I have more or less calculated math, but I wonder if there is an easier way to do this using DateTime stuff?

+2
source share
1 answer

I recommend that you make your question clearer; It took me a while to figure out what 0.14285you referred to represented a fraction of the day.

You can use something like this:

 // {03:25:42.24}
TimeSpan unSnapped = TimeSpan.FromDays(0.14285);

 // {04:00:00} : Round up the hours and construct a TimeSpan from it
TimeSpan snapped = TimeSpan.FromHours(Math.Ceiling(unSnapped.TotalHours));

// 0.166666666
double snappedFractionOfADay = snapped.TotalDays;

You can check the properties TotalXXXat time intervals to get other information that you provide, if necessary.

+4
source

All Articles