Extension methods to verify a datetime in a range


/ Published in: C#
Save to your folder(s)

This snippets regards a couple of extension methods to verify if a specified datetime is into a range.


Copy this code and paste it in your HTML
  1. #region DATETIME
  2. /// <summary>
  3. /// Verify if a datetime is in a range
  4. /// </summary>
  5. /// <param name="dateToCompare">The date to compare</param>
  6. /// <param name="Left">The left datetime in the range</param>
  7. /// <param name="Right">The right datetime in the range</param>
  8. /// <remarks>The range will include the left and the right datetime specified</remarks>
  9. /// <returns>Returns true if the datetime compared is into the range specified</returns>
  10. public static bool BetweenInclusive(this DateTime dateToCompare, DateTime Left, DateTime Right)
  11. {
  12. bool result = (dateToCompare.CompareTo(Left) >= 0) && (dateToCompare.CompareTo(Right) <= 0);
  13. return result;
  14. }
  15.  
  16. /// <summary>
  17. /// Verify if a datetime is in a range
  18. /// </summary>
  19. /// <param name="dateToCompare">The date to compare</param>
  20. /// <param name="Left">The left datetime in the range</param>
  21. /// <param name="Right">The right datetime in the range</param>
  22. /// <remarks>The range will exclude the left and the right datetime specified</remarks>
  23. /// <returns>Returns true if the datetime compared is into the range specified</returns>
  24. public static bool BetweenExclusive(this DateTime dateToCompare, DateTime Left, DateTime Right)
  25. {
  26. bool result = (dateToCompare.CompareTo(Left) > 0) && (dateToCompare.CompareTo(Right) < 0);
  27. return result;
  28. }
  29. #endregion

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.