SHARE
TWEET

DateTime Round, Floor, and Ceiling

ForeverZer0 Jul 4th, 2014 227 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private static DateTime Floor(DateTime dateTime, TimeSpan interval)
  2. {
  3.   return dateTime.AddTicks(-(dateTime.Ticks % interval.Ticks));
  4. }
  5.  
  6. private static DateTime Ceiling(DateTime dateTime, TimeSpan interval)
  7. {
  8.   return dateTime.AddTicks(interval.Ticks - (dateTime.Ticks % interval.Ticks));
  9. }
  10.  
  11. private static DateTime Round(DateTime dateTime, TimeSpan interval)
  12. {
  13.   var halfIntervelTicks = (interval.Ticks + 1) >> 1;
  14.  
  15.   return dateTime.AddTicks(halfIntervelTicks - ((dateTime.Ticks + halfIntervelTicks) % interval.Ticks));
  16. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top