Want more features on Pastebin? Sign Up, it's FREE!
Guest

Is there a better way in C# to round a DateTime to the nearest 5 seconds

By: a guest on Feb 11th, 2012  |  syntax: None  |  size: 2.13 KB  |  views: 102  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. DateTime now = DateTime.Now;
  2. int second = 0;
  3.  
  4. // round to nearest 5 second mark
  5. if (now.Second % 5 > 2.5)
  6. {
  7.     // round up
  8.     second = now.Second + (5 - (now.Second % 5));
  9. }
  10. else
  11. {
  12.     // round down
  13.     second = now.Second - (now.Second % 5);
  14. }
  15.  
  16. DateTime rounded = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, second);
  17.        
  18. DateTime now = DateTime.Now;
  19.   DateTime rounded = new DateTime(((now.Ticks + 25000000) / 50000000) * 50000000);
  20.        
  21. public static TimeSpan Round(this TimeSpan time, TimeSpan roundingInterval, MidpointRounding roundingType) {
  22.         return new TimeSpan(
  23.             Convert.ToInt64(Math.Round(
  24.                 time.Ticks / (decimal)roundingInterval.Ticks,
  25.                 roundingType
  26.             )) * roundingInterval.Ticks
  27.         );
  28.     }
  29.  
  30.     public static TimeSpan Round(this TimeSpan time, TimeSpan roundingInterval) {
  31.         return Round(time, roundingInterval, MidpointRounding.ToEven);
  32.     }
  33.  
  34.     public static DateTime Round(this DateTime datetime, TimeSpan roundingInterval) {
  35.         return new DateTime((datetime - DateTime.MinValue).Round(roundingInterval).Ticks);
  36.     }
  37.        
  38. new DateTime(2010, 11, 4, 10, 28, 27).Round(TimeSpan.FromMinutes(1)); // rounds to 2010.11.04 10:28:00
  39. new DateTime(2010, 11, 4, 13, 28, 27).Round(TimeSpan.FromDays(1)); // rounds to 2010.11.05 00:00
  40. new TimeSpan(0, 2, 26).Round(TimeSpan.FromSeconds(5)); // rounds to 00:02:25
  41. new TimeSpan(3, 34, 0).Round(TimeSpan.FromMinutes(37); // rounds to 03:42:00...for all your round-to-37-minute needs
  42.        
  43. static int Round(int n, int r)
  44. {
  45.     if ((n % r) <= r / 2)
  46.     {
  47.         return n - (n % r);
  48.     }
  49.     return n + (r - (n % r));
  50. }
  51.        
  52. DateTime rounded = roundTo5Secs (DateTime.Now);
  53.        
  54. secBase = now.Second / 5;
  55. secExtra = now.Second % 5;
  56. if (secExtra > 2) {
  57.     return new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute,
  58.         secBase + 5);
  59. }
  60. return new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute,
  61.     secBase);
  62.        
  63. // truncate to multiple of 5
  64. int second = 5 * (int) (now.Second / 5);
  65. DateTime dt = new DateTime(..., second);
  66.  
  67. // round-up if necessary
  68. if (now.Second % 5 > 2.5)
  69. {
  70.     dt = dt.AddSeconds(5);
  71. }
clone this paste RAW Paste Data