From the course: Visual Studio Developer Tips

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Date rounding and other helpers

Date rounding and other helpers - Visual Studio Tutorial

From the course: Visual Studio Developer Tips

Date rounding and other helpers

- [Instructor] Rounding numbers is a common task for numeric values. This tip looks at how to create a rounding library for date and time values. Here's the idea. You need a minute value, but you are not interested in the second portion of the time value. With rounding, values below 30 seconds are rounded to the current minute, values above 30 are rounded to the next minute. The code for this tip is implemented as an extension method. I'll start off by looking at extension methods plus show you some other simple and effective date helpers. The code for this example is in DateTimeExtensions. The idea behind a extension method is that you're going to create a method that appears to be part of a type even though it's not in that type definition. In C#, the way you do that is you create a static class like I have here, and then inside your static class you create a method that's also static, like this. To make it an extension method it has to be marked as static and then you also need to…

Contents