Created
March 4, 2015 06:56
-
-
Save danielmackay/9fd5e475c544f6a1a06c to your computer and use it in GitHub Desktop.
Date Time helper methods for getting the start/end date of the week/month/year. #datetime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [TestClass()] | |
| public class DateTimeExtTests | |
| { | |
| private const int CurrentYear = 2015; | |
| private const int CurrentMonth = 3; | |
| private const int CurrentDay = 4; | |
| private DateTime CurrentDate | |
| { | |
| get { return new DateTime(CurrentYear, CurrentMonth, CurrentDay); } | |
| } | |
| [TestMethod()] | |
| public void StartOfMonthTest() | |
| { | |
| var result = CurrentDate.StartOfMonth(); | |
| result.Year.Should().Be(CurrentYear); | |
| result.Month.Should().Be(CurrentMonth); | |
| result.Day.Should().Be(1); | |
| } | |
| [TestMethod()] | |
| public void EndOfMonthTest() | |
| { | |
| var result = CurrentDate.EndOfMonth(); | |
| result.Year.Should().Be(CurrentYear); | |
| result.Month.Should().Be(CurrentMonth); | |
| result.Day.Should().Be(31); | |
| } | |
| [TestMethod()] | |
| public void StartOfWeekTest() | |
| { | |
| var result = CurrentDate.StartOfWeek(DayOfWeek.Monday); | |
| result.Year.Should().Be(CurrentYear); | |
| result.Month.Should().Be(CurrentMonth); | |
| result.Day.Should().Be(2); | |
| } | |
| [TestMethod()] | |
| public void EndOfWeekTest() | |
| { | |
| var result = CurrentDate.EndOfWeek(DayOfWeek.Monday); | |
| result.Year.Should().Be(CurrentYear); | |
| result.Month.Should().Be(CurrentMonth); | |
| result.Day.Should().Be(8); | |
| } | |
| [TestMethod()] | |
| public void StartOfYearTest() | |
| { | |
| var result = CurrentDate.StartOfYear(); | |
| result.Year.Should().Be(CurrentYear); | |
| result.Month.Should().Be(1); | |
| result.Day.Should().Be(1); | |
| } | |
| [TestMethod()] | |
| public void EndOfYearTest() | |
| { | |
| var result = CurrentDate.EndOfYear(); | |
| result.Year.Should().Be(CurrentYear); | |
| result.Month.Should().Be(12); | |
| result.Day.Should().Be(31); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment