The book picks up where we left off, and we continue to share some common extensions for special time acquisition.
01Get the first specified day of the week of the month in which the current date falls.
The method and the first day of the week introduced earlier to get the current date of the first day of the week (Monday) the core idea is the same, just ask for Monday to ask for the number of weeks, of course, some of the small details need to pay attention to, such as the first day of the week to ask for the two days of the week are in the same week, and the first week of the month to ask for the first week of the month may be two days in a different week, the specific code is as follows:
/ / Get the current date of the month of the first specified day of the week
public static DateTime GetFirstDayOfWeekDateTimeInMonth(this DateTime dateTime, DayOfWeek dayOfWeek)
{
// Get the first day of the month of the current date
var firstDayOfMonth = ();
// Calculate the number of days difference between the target date and the first day of the month
var diff = ((int)dayOfWeek - (int) + 7) % 7;
return (diff);
}
Below we also need to do detailed unit tests, we test specifying two special days, Monday and Sunday, and then three special cases respectively:
Designate Mondays for testing:
(1) Verify that the current date is Friday and Monday is in the following week;
(2) Verify that the current date is the first Monday of the month;
(3) Verify that the current date is a Sunday and is after the first Monday of the month;
Designated Sunday Tests:
(1) Verify that the current date is Friday and before the first Sunday of the month;
(2) Verify that the current date is the first Sunday of the month;
(3) Verify that the current date is Monday and after the first Sunday of the month;
The specific code is as follows:
[Fact]
public void GetFirstDayOfWeekDateTimeInMonth()
{
// Verify that the current date is Friday and Monday is in the following week
var friday_monday = new DateTime(2024, 11, 1, 14, 10, 10);
var day_friday_monday = friday_monday.GetFirstDayOfWeekDateTimeInMonth();
(new DateTime(2024, 11, 4), day_friday_monday); var day_friday_monday = friday_monday.
// Verify that the current date is the first Monday of the month
var monday_monday = new DateTime(2024, 11, 4, 4, 10, 10);
var day_monday_monday = monday_monday.GetFirstDayOfWeekDateTimeInMonth(); var day_monday_monday = monday_monday.
(new DateTime(2024, 11, 4), day_monday_monday);
// Verify that the current date is a Sunday and after the first Monday of the month
var sunday_monday = new DateTime(2024, 11, 30, 4, 10, 10);; //verify that the current date is a Sunday and after the first Monday of the month
var day_sunday_monday = sunday_monday.GetFirstDayOfWeekDateTimeInMonth(); var day_sunday_monday = sunday_monday.
(new DateTime(2024, 11, 4), day_sunday_monday); var day_sunday_monday = sunday_monday.
// Verify that the current date is Friday and before the first Sunday of the month
var friday_sunday = new DateTime(2024, 11, 1, 14, 10, 10);
var day_friday_sunday = friday_sunday.GetFirstDayOfWeekDateTimeInMonth();
(new DateTime(2024, 11, 3), day_friday_sunday); var day_friday_sunday = friday_sunday.
// Verify that the current date is the first Sunday of the month
var sunday_sunday = new DateTime(2024, 11, 30, 4, 10, 10);
var day_sunday_sunday = sunday_sunday.GetFirstDayOfWeekDateTimeInMonth(); var day_sunday_sunday = sunday_sunday.
(new DateTime(2024, 11, 3), day_sunday_sunday);
// Verify that the current date is Monday and after the first Sunday of the month
var monday_sunday = new DateTime(2024, 11, 4, 4, 10, 10);
var day_monday_sunday = monday_sunday.GetFirstDayOfWeekDateTimeInMonth(); var day_monday_sunday = monday_sunday.
(new DateTime(2024, 11, 3), day_monday_sunday); var day_monday_sunday = monday_sunday.
}
02Get the last specified day of the week of the month in which the current date falls.
The method and the previous request for the first specified day of the week the core idea is the same, the specific code is as follows:
/ / Get the current date of the month of the last specified day of the week
public static DateTime GetLastDayOfWeekDateTimeInMonth(this DateTime dateTime, DayOfWeek dayOfWeek)
{
// Get the last day of the month in which the current date falls.
var lastDayOfMonth = ();
// Calculate the number of days between the target date and the last day of the month
var diff = ((int) - (int)dayOfWeek + 7) % 7;
return (-diff);
}
You can refer to the unit test to find the first specified day of the week, so I won't go into that here.
03Get the day of the week preceding the current date.
Seek the last specified day of the week, in fact, it is not complicated, first calculate the current date and the target day of the week difference in the number of days, of which there is a small detail need to pay attention to, that is, if the two dates are the same, then you need to change the difference in the number of days to 7, the specific code is as follows:
/ / Get the current date on a specified day of the week
public static DateTime GetPreviousDayDateTimeOfWeek(this DateTime dateTime, DayOfWeek dayOfWeek)
{
// Calculate the number of days difference between the current date and the target day of the week.
var diff = ((int) - (int)dayOfWeek + 7) % 7;
//If the difference of 0 days means that the current date is the same as the target day of the week, it needs to be changed to 7.
diff = diff == 0 ? 7 : diff; return (-diff).
return (-diff).Date.
}
We do unit testing for each of the following four scenarios:
(1) Verify that the current date is a Monday and the previous Monday was in the previous month;
(2) Verify that the current date is a Monday and the previous Monday is in the current month;
(3) Verify that the current date is a Sunday and the previous Monday is in the current month;
(4) Verify that the current date is Saturday and is the last day of the month;
The specific code is as follows:
[Fact]
public void GetPreviousDayDateTimeOfWeek()
{
// Verify that the current date is a Monday and the previous Monday was in the previous month
var monday = new DateTime(2024, 11, 1, 14, 10, 10);
var day_monday = ();
(new DateTime(2024, 10, 28), day_monday);
//Verify that the current date is Monday and the previous Monday is in the current month
var monday1 = new DateTime(2024, 11, 25, 14, 10, 10);; //verify that the current date is Monday and the previous Monday is in the current month
var day_monday1 = ();
(new DateTime(2024, 11, 18), day_monday1);
// Verify that the current date is a Sunday and the previous Monday is in the current month
var sunday = new DateTime(2024, 11, 24, 4, 10, 10);
var day_sunday = ();
(new DateTime(2024, 11, 18), day_sunday);
// Verify that the current date is Saturday and is the last day of the month
var saturday = new DateTime(2024, 11, 30, 4, 10, 10);
var day_saturday = ();
(new DateTime(2024, 11, 25), day_saturday); var saturday = new DateTime(2024, 11, 30, 4, 10, 10)
}
04Get the next specified day of the week after the current date.
The method and the above to get the last specified day of the week core idea is the same, the specific code is as follows:
/ / Get the current date of the next most recent specified day of the week
public static DateTime GetNextDayDateTimeOfWeek(this DateTime dateTime, DayOfWeek dayOfWeek)
{
//Calculate the target date and the last day of the month difference in days
var diff = ((int)dayOfWeek - (int) + 7) % 7;
//If the difference of 0 days means that the current date and the target day of the week are the same, it needs to be changed to 7
diff = diff == 0 ? 7 : diff; return (diff).
return (diff).Date.
}
The unit test can also be referred to to find the last specified day of the week, so I won't go into that here.
05Get the week of the month in which the current date falls.
The core idea of the method is to get the current date and the first day of the month how many days difference, and then divided by the number of days difference by 7 to get the current is the first week.
But here is a more troublesome thing is if the first week is less than a week it, for example, the first day of the first week of the month is 2024-11-01 Friday, and today is 2024-11-07 Thursday, should be the second week of the month, but if you directly calculate the difference between the two days and then divide the result by 7 results are obviously not right.
So we first need to make up the number of days in the first week that are less than a week, i.e. there are 4 days ahead.
In this way it is (7+4)/7 = 1...4, which is the second week in which it is located, where the quotient denotes a complete week and the remainder denotes an incomplete week. If converted into a formula it is: days/7 + (days%7 > 0 ? 1 : 0), we simplify this formula to get: (days + 6)/7, the specific implementation code is as follows:
// get the current date is the first week of its month
public static int GetWeekOfMonth(this DateTime dateTime)
{
//Get the current date of the first day of the month
var firstDayOfMonth = (); // First set Monday as the first day of the week.
//First set Monday as the start of the week.
//calculate the number of days between the first day of the current month and Monday
//that is, if the first week is less than a week how many days are missing
var diff = ((int) - (int) + 7) % 7; //use the difference between the first week of the month and the first week of the month to calculate the number of days.
// Calculate the current week of the month by adding the difference between the first week and the current day to the sum of the days in the first week.
//Then calculate the quotient of the total number of days/7, and add 1 if there is a remainder.
//formula for: n/7 + (n%7 > 0 ? 1 : 0)
//The above formula can be simplified to (n+6)/7
return (diff + + + 6) / 7;
}
Below we perform detailed unit tests for the following scenarios:
(1) Verify that the current date is Friday and is the first day of the month;
(2) Verify that the current date is a Sunday and in the first week of the month;
(3) Verify that the current date is Monday and in the third week of the month;
(4) Verify that the current date is a Sunday and in the third week of the month;
(5) Verify that the current date is a Saturday and is the last day of the month;
The specific code is as follows:
[Fact]
public void GetWeekOfMonth()
{
// Verify that the current date is Friday and is the first day of the month
var Friday = new DateTime(2024, 11, 1, 14, 10, 10);
var day_friday = ();
(1, day_friday);
// Verify that the current date is a Sunday and in the first week of the month
var sunday = new DateTime(2024, 11, 3, 14, 10, 10);
var day_sunday = ();
(1, day_sunday);
// verify that the current date is Monday and in the third week of the month
var monday = new DateTime(2024, 11, 11, 4, 10, 10);
var day_monday = ();
(3, day_monday);
// Verify that the current date is a Sunday and in the third week of the month
var date17 = new DateTime(2024, 11, 17, 4, 10, 10);
var day17 = ();
(3, day17);
//Verify that the current date is a Saturday and is the last day of the month
var sunday1 = new DateTime(2024, 11, 30, 4, 10, 10);
var day_sunday1 = ();
(5, day_sunday1);
}
06The current date is the first week of the year (ISO 8601 standard).
In the ISO 8601 standard, the week begins on a Monday and there are a minimum of 52 weeks in a year, with the first week of the year being the week containing the first day of the year, and the week must have at least four days.
Getting the week of the year in which the date is of course can be done by calling the GetWeekOfYear method in the Calendar component of the Cultural Information in C#, the specific code is as follows:
/ / to get the current date is the first week of the year it is (ISO 8601 standard)
public static int GetWeekOfYear(this DateTime dateTime)
var currentCulture = ;)
var currentCulture = ;
return (dateTime, , );
}
07Get the number of weeks in the month in which the current date falls.
The core idea of the method is to first get the current date of the first day of the month and the last day of the month, and then calculate the first few weeks of the year they are in, and finally subtracted to get the specific code is as follows:
// get the current date in the month of the number of weeks
public static int GetWeeksInMonth(this DateTime dateTime)
{
// get the current date of the first day of the month
var firstDayOfMonth = (); //Get the first day of the month in which the current date is located.
//Get the last day of the month of the current date
var lastDayOfMonth = (); //Get the first day of the month in which the current date falls.
//Get the number of weeks in the year for the first day of the month
var firstWeek = ();
//Get the number of weeks in the year on the last day of the month.
var lastWeek = (); //get the number of weeks in the year on the last day of the month
return lastWeek - firstWeek + 1; }
}
08, determine whether the current date is a weekend or not
The method is relatively simple, just determine whether the current is a Saturday or Sunday, the specific code is as follows:
// determine whether the current date is the weekend
public static bool IsWeekend(this DateTime dateTime)
{
return == || == ;
}
09, determine whether the current date is in a leap year or not
The method calls the C# built-in method IsLeapYear with the following code:
// determine whether the current date of the year is a leap year
public static bool IsLeapYear(this DateTime dateTime)
{
return ();
}
10Get the quarter in which the current date falls.
The method is also relatively simple, only need to apply a small formula can be sought, the specific code is as follows:
// get the current date in the quarter
public static int GetQuarter(this DateTime dateTime)
{
return ( - 1) / 3 + 1; }
}
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