| | | 1 | | namespace ArturRios.Extensions; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Provides extension methods for DateTime, such as helpers to remove milliseconds precision. |
| | | 5 | | /// </summary> |
| | | 6 | | public static class DateTimeExtensions |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Returns the same DateTime truncated to whole seconds, preserving the Kind. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <param name="dateTime">The DateTime to normalize.</param> |
| | | 12 | | /// <returns>A DateTime truncated to seconds.</returns> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// Every sub-second component is dropped, not only the milliseconds: microseconds and the remaining |
| | | 15 | | /// ticks go with them. Truncation is towards zero, so a value is never rounded up to the next second. |
| | | 16 | | /// </remarks> |
| | | 17 | | public static DateTime RemoveMilliseconds(this DateTime dateTime) => |
| | 14 | 18 | | new( |
| | 14 | 19 | | dateTime.Year, |
| | 14 | 20 | | dateTime.Month, |
| | 14 | 21 | | dateTime.Day, |
| | 14 | 22 | | dateTime.Hour, |
| | 14 | 23 | | dateTime.Minute, |
| | 14 | 24 | | dateTime.Second, |
| | 14 | 25 | | dateTime.Kind); |
| | | 26 | | } |