Skill Level Intermediate
- [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 use the this keyword for the parameter, the first parameter, like that.
This parameter, and this is hard to say because the keyword is this and and is also taken by the parameter, uses this keyword in front of it and that makes this an extension method. So whenever there's an instance of DateTime type, you can use this FirstDayOfMonth method. So the idea here is we're going to create a helper method that returns the first day of the month. Now this isn't that hard a code to write but this can be helpful. So I'll just say return new DateTime, pick the current date, we'll get the Year for that, and then we'll get the date Month, and then finally the Day which I'll just set to one.
A companion method for this, of course, will be the LastDayOfMonth. Finish that, the LastDayOfMonth, and then the code has to change slightly. I'm going to return a new DateTime and we'll do the same date.Year, same Month. There's a couple of ways you could implement this, and this is how I'm going to implement it. I get the number of days in the month of the year and month, so if it's July 2017, it'll tell me that there are 31 days in that month.
And then I'll return that as the day for this LastDayOfMonth. So now I can go over here to another bit of code, and we'll uncomment this, and now I can say I get the FirstDayOfMonth. And here's the key part of extension methods. I'm calling DateTime now, that's returning an instance of a DateTime, and then that allows me to use my extension method. So now that we've seen the basics of a DateTime extension method, let's look at a couple of extra ones here that I've written.
Here's one call AddWeekdays. This is really helpful because if you add days, this is going to add 12 days. So since I'm recording this on a Monday, that would add 12 days to Monday and also count Saturday and Sunday. Instead what I wanted to have is a method called AddWeekdays, so this will skip Saturday and Sunday, and that code is over here. And I won't step through the code and discuss how it works, but that's another interesting extension method.
Now let's get onto the rounding code. The way this works is I've got a round unit enumeration here so you'll be able to round up to Second, here's Minute, there's Hour or there's Day. And you're going to call this Round method like this. You're going to pass in the candidate date and then the rounding Unit. So you'd call it like this. I'll say DateTime.Now.Round, and then round to the nearest minute. And then the code in here has a switch statement that says if the rounding Unit is a Second, then send back a DateTime based on the current value that was passed in, and it's here, the candidate, so the candidate Year, Month, Day, Minute, Hour, and Second.
And then if the candidate.Millisecond is greater than or equal to 500, then we'll round it up by adding one second to it. Then in the next case, you do the same thing for minute, only this time we say if the Second is greater than or equal to 30, then we add a minute. And you get the idea. We round the hour if it's greater than 30 minutes, then we add an hour. And finally down here, if it's greater than 12 hours, then we add a day. And let's go ahead and put a break point here, press F9, make sure my console app is my startup application.
And we'll take a look at the roundedMinute, and you can see that it has rounded it to 25:00. Now that you've seen how to make helper methods for DateTime values, consider making some custom methods for your own application.
Q: Why can't I earn a Certificate of Completion for this course?
A: We publish a new tutorial or tutorials for this course on a regular basis. We are unable to offer a Certificate of Completion because it is an ever-evolving course that is not designed to be completed. Check back often for new movies.