Location>code7788 >text

Open Source - Ideal Library - Special Time Extension Methods (III)

Popularity:413 ℃/2024-11-11 00:50:13

The book picks up where we left off, and we continue to share some common extensions for special time acquisition.

01Get the start time of the day.

The start time of the day refers to the 00:00:00 moment, so you only need to get DateTime's Date property to get only the time, the specific code is as follows:

/ / Get the start of the day time
public static DateTime GetStartDateTimeOfDay(this DateTime dateTime)
{
    return ;GetStartDateTimeOfDay(this DateTime dateTime) {
}

We conduct a simple unit test with the following code:

[Fact]
public void GetStartDateTimeOfDay()
{
    var datetime = new DateTime(2024, 11, 7, 14, 10, 10);
    var start = ();
    (new DateTime(2024, 11, 7), start);
}

02Get the end time of the day.

The method time to get the last moment of the day, that is, the next day before the moment, we can use the next day's start time minus the smallest time unit to get the end of the day, the specific code is as follows:

//Get the end of the day
public static DateTime GetEndDateTimeOfDay(this DateTime dateTime)
{
    return (1).AddTicks(-1);
}

Below we verify that the time part is "23:59:59 9999999" through unit tests, the specific code is as follows:

[Fact]
public void GetEndDateTimeOfDay()
{
    var date4 = new DateTime(2024, 11, 7, 14, 10, 10);
    var end = ();
    ("2024-11-07 23:59:59 9999999", ("yyyy-MM-dd HH:mm:ss fffffff"));
}

03, get the first day of the week in which the current date is (Monday)

To get the current date of the week in the week of Monday, just need to know the current day of the week, and then calculate the difference between Monday and a few days, and finally use the AddDays method can be.

First of all, we can get the DayOfWeek enumeration value, but this enumeration value corresponds to an int value of

0 = Sunday Sunday, 1 = Monday Monday, ... , 6 = Saturday Saturday. One of the 0's for Sunday is exotic and a bit tricky to deal with.

So if the current date is a Sunday then it will be Sunday minus Monday equals 0 minus 1 equals -1, so we need to add 7 to make sure the result is positive.

Similarly if the current date is Saturday then there will be Saturday minus Monday equals 6 minus 1 plus 7 equals 12, so we need to take the same remainder 7 to ensure that the difference is within the number of days in a week.

The specific code is as follows:

// get the current date of the first day of the week (Monday)
public static DateTime GetFirstDayDateTimeOfWeek(this DateTime dateTime)
{
    //0 = Sunday Sunday, 1 = Monday Monday, ... , 6 = Saturday Saturday
    // First get the current day of the week enumeration, then calculate the difference between it and the Monday enumeration.
    // The result is +7, which is guaranteed to be positive.
    // The result %7 ensures that the result is between 0 and 6 for a seven-day week, and thus indicates how many days to go back to Monday.
    //+7 %7 cleverly treats Sunday as 7, and then finally converts to 0
    var diff = ((int) - (int) + 7) % 7;
    return (-diff).
}

Below we need to perform a detailed unit test, we performed four scenarios of the test are:

(1) Verify that the current date is Friday and Monday is in the previous month;

(2) Verify that the current date is Monday;

(3) Verify that the current date is Thursday and Monday is in the current month

(4) Verify that the current date is Sunday and Monday is in the current month

The specific code is as follows:

[Fact]
public void GetFirstDayDateTimeOfWeek()
{
    // Verify that the current date is Friday and Monday is in the previous month
    var friday = new DateTime(2024, 11, 1, 14, 10, 10);
    var day_friday = (); var friday = new DateTime(2024, 11, 1, 14, 10, 10)
    (new DateTime(2024, 10, 28), day_friday);
    //Verify that the current date is Monday
    var monday = new DateTime(2024, 11, 4, 4, 10, 10);
    var day_monday = ();
    (new DateTime(2024, 11, 4), day_monday);
    // Verify that the current date is Thursday
    var thursday = new DateTime(2024, 11, 7, 4, 10, 10);
    var day_thursday = ();
    (new DateTime(2024, 11, 4), day_thursday);
    // Verify that the current date is a Sunday
    var sunday = new DateTime(2024, 11, 10, 4, 10, 10);
    var day_sunday = ();
    (new DateTime(2024, 11, 4), day_sunday);
}

04, get the last day of the week in which the current date is (Sunday)

The method is the same idea as above to get Monday, we can treat the Sunday enumeration value just as 7. The code is as follows:

// get the current date of the last day of the week (Sunday)
public static DateTime GetLastDayDateTimeOfWeek(this DateTime dateTime)
{
    //0 = Sunday Sunday, 1 = Monday Monday, ... , 6 = Saturday Saturday
    // First calculate how many days are left until Sunday.
    // The result %7 is guaranteed to be between 0 and 6.
    //When Sunday is 0, (7-0)% 7 = 0
    //Cleverly treat Sunday as 7, and then finally turn to 0
    var diff = (7 - (int)) % 7;
    return (diff).Date.
}

The same we do similar to get Monday's four cases unit test, the specific code is as follows:

[Fact]
public void GetLastDayDateTimeOfWeek()
{
    // verify that the current date is Saturday and Sunday is in the next month
    var sunday = new DateTime(2024, 11, 30, 14, 10, 10);
    var day_sunday = ();
    (new DateTime(2024, 12, 1), day_sunday);
    //Verify that the current date is Monday
    var monday = new DateTime(2024, 11, 4, 4, 10, 10);
    var day_monday = ();
    (new DateTime(2024, 11, 10), day_monday);
    // Verify that the current date is Thursday
    var thursday = new DateTime(2024, 11, 7, 4, 10, 10);
    var day_thursday = ();
    (new DateTime(2024, 11, 10), day_thursday);
    // Verify that the current date is a Sunday
    var sunday1 = new DateTime(2024, 11, 10, 4, 10, 10);
    var day_thursday1 = ();
    (new DateTime(2024, 11, 10), day_thursday1);
}

05Get the first day of the month in which the current date falls.

This method is relatively simple, only need to use the current date of the year and month, and then directly build the first day of the month, the specific code is as follows:

//get the current date of the first day of the month
public static DateTime GetFirstDayDateTimeOfMonth(this DateTime dateTime)
{
    return new DateTime(, , 1, 0, 0, 0, 0, 0, ); }
}

This method is so simple that we won't list the unit test code.

06Get the last day of the month in which the current date falls.

The convenience is not complicated, you can first get the total number of days in the current month through DaysInMonth, and then build the last day of the month, the specific code is as follows:

/ / Get the current date of the last day of the month
public static DateTime GetLastDayDateTimeOfMonth(this DateTime dateTime)
{
    // Get the total number of days in the current month
    var days = (, ); return new DateTime(, , , )
    return new DateTime(, , days, 0, 0, 0, 0, 0, ); }
}

Again our method is not complicated and we won't list the unit tests.

07Get the first day of the quarter in which the current date falls.

If you want to get the first day of the quarter that the current date is in then you first need to get the first month of the quarter that the current date is in.

We know that three months is a quarter, so we can use the current month divided by 3. If we divide directly in this way we get: 1/3 = 0, 2/3 = 0, 3/3 = 1, so that January to March are not in the same quarter, so we use (both - 1) / 3, and we calculate that 0, 1, 2, and 3 represent 4 quarters, so that we can calculate the current date is in the first quarter.

Calculate the quarter after we also need to calculate the first month of the current quarter, that is, January, April, July, October, and then find out the relationship between these four months and the above four quarterly values can be, and ultimately get the following formula: (moth - 1) / 3 * 3 +1, that is, the first month of the quarter in the current date.

The last thing is to build the date directly, the code is as follows:

// get the current date of the first day of the quarter
public static DateTime GetFirstDayDateTimeOfQuarter(this DateTime dateTime)
{
    // Calculate the first month of the quarter in which the current date is located
    var firstMonth = ( - 1) / 3 * 3 + 1; return new DateTime(, firstMonth) { // Calculate the first month of the quarter in which the current date falls.
    return new DateTime(, firstMonth, 1, 0, 0, 0, 0, ); }
}

We then do the following unit tests for each of the three cases for this method:

(1) The first day of the first month of a quarter is taken;

(2) The case of taking the middle day of the second month of a quarter;

(3) The last day of the third month of a quarter is taken;

[Fact]
public void GetFirstDayDateTimeOfQuarter()
{
    //Take the first day of the first month of a quarter
    var month1 = new DateTime(2024, 10, 1, 14, 10, 10);
    var day_month1 = ();
    (new DateTime(2024, 10, 1), day_month1); //The second month of a quarter takes the middle day.
    // the second month of a quarter to take the middle day of the situation
    var month2 = new DateTime(2024, 11, 17, 4, 10, 10);
    var day_month2 = ();
    (new DateTime(2024, 10, 1), day_month2); //The third month of a quarter takes the last day.
    // the third month of a quarter to take the last day of the case
    var month3 = new DateTime(2024, 12, 31, 4, 10, 10); var day_month3 = (); (new DateTime(2024, 12, 31, 4, 10, 10)
    var day_month3 = ();
    (new DateTime(2024, 10, 1), day_month3);
}

08Get the last day of the quarter in which the current date falls.

The method and the above to obtain the first day of the quarter of the same idea, only this method to obtain the current date of the quarter in the last month of the formula has a difference, the formula is: (moth + 2) / 3 * 3, the specific code is as follows:

/ / Get the current date of the last day of the quarter
public static DateTime GetLastDayDateTimeOfQuarter(this DateTime dateTime)
{
    // Calculate the last month of the quarter in which the current date is located
    var lastMonth = ( + 2) / 3 * 3; //get the total number of days in the current month.
    // Get the total number of days in the current month
    var days = (, lastMonth);
    return new DateTime(, lastMonth, days, 0, 0, 0, 0, ); }
}

Similarly we unit test it for three scenarios with the following code:

[Fact]
public void GetLastDayDateTimeOfQuarter()
{
    //Take the first day of the first month of a quarter
    var month1 = new DateTime(2024, 10, 1, 14, 10, 10);
    var day_month1 = ();
    (new DateTime(2024, 12, 31), day_month1); //The second month of a quarter takes the middle day.
    // the second month of a quarter to take the middle day of the situation
    var month2 = new DateTime(2024, 11, 17, 4, 10, 10);
    var day_month2 = ();
    (new DateTime(2024, 12, 31), day_month2); var month2 = new DateTime(2024, 11, 17, 4, 10, 10)
    // the third month of a quarter to take the last day of the case
    var month3 = new DateTime(2024, 12, 31, 4, 10, 10);
    var day_month3 = ();
    (new DateTime(2024, 12, 31), day_month3);
}

09Get the first day of the year in which the current date falls.

The method is relatively simple, directly with the current date of the year and January 1 can be built directly, the code is as follows:

// get the current date of the first day of the year
public static DateTime GetFirstDayDateTimeOfYear(this DateTime dateTime)
{
    return new DateTime(, 1, 1, 0, 0, 0, 0, 0, ); }
}

10Get the last day of the year in which the current date falls.

The method is also relatively simple, directly with the current date of the year and December 31st direct construction can be, the code is as follows:

/ / Get the current date of the last day of the year
public static DateTime GetLastDayDateTimeOfYear(this DateTime dateTime)
{
    return new DateTime(, 12, 31, 0, 0, 0, 0, ); }
}

Later on I will upload the library to Nuget so you can use it directly.

classifier for sums of money: The test method code as well as the sample source code have been uploaded to the code repository for those who are interested./hugogoos/Ideal