我如何得到一个月的最后一天?

时间:2022-10-16 08:39:42

How can I find the last day of the month in C#?

如何在c#中找到一个月的最后一天?

For example, if I have the date 03/08/1980, how do I get the last day of month 8 (in this case 31)?

例如,如果我有日期03/08/1980,我如何获得第8个月的最后一天(在本例中是31)?

10 个解决方案

#1


446  

The last day of the month you get like this, which returns 31:

一个月的最后一天,你会得到31:

DateTime.DaysInMonth(1980, 08);

#2


143  

var lastDayOfMonth = DateTime.DaysInMonth(date.Year, date.Month);

#3


66  

DateTime firstOfNextMonth = new DateTime(date.Year, date.Month, 1).AddMonths(1);
DateTime lastOfThisMonth = firstOfNextMonth.AddDays(-1);

#4


23  

If you want the date, given a month and a year, this seems about right:

如果你想要一个月或一年的日期,这似乎是正确的:

public static DateTime GetLastDayOfMonth(this DateTime dateTime)
{
    return new DateTime(dateTime.Year, dateTime.Month, DateTime.DaysInMonth(dateTime.Year, dateTime.Month));
}

#5


9  

Substract a day from the first of next month:

从下个月1日起减除一天:

DateTime lastDay = new DateTime(MyDate.Year,MyDate.Month+1,1).AddDays(-1);

Also, in case you need it to work for December too:

另外,如果你也需要它在12月份工作:

DateTime lastDay = new DateTime(MyDate.Year,MyDate.Month,1).AddMonths(1).AddDays(-1);

#6


8  

You can find the last day of the month by a single line of code:

你可以通过一行代码找到一个月的最后一天:

int maxdt = (new DateTime(dtfrom.Year, dtfrom.Month, 1).AddMonths(1).AddDays(-1)).Day;

#7


6  

You can find the last date of any month by this code:

你可于下列日期找到每个月的最后日期:

var now = DateTime.Now;
var startOfMonth = new DateTime(now.Year, now.Month, 1);
var DaysInMonth = DateTime.DaysInMonth(now.Year, now.Month);
var lastDay = new DateTime(now.Year, now.Month, DaysInMonth);

#8


3  

From DateTimePicker:

从DateTimePicker:

First date:

第一次约会:

DateTime first_date = new DateTime(DateTimePicker.Value.Year, DateTimePicker.Value.Month, 1);

Last date:

最后的日期:

DateTime last_date = new DateTime(DateTimePicker.Value.Year, DateTimePicker.Value.Month, DateTime.DaysInMonth(DateTimePicker.Value.Year, DateTimePicker.Value.Month));

#9


1  

I don't know C# but, if it turns out there's not a convenient API way to get it, one of the ways you can do so is by following the logic:

我不知道c#,但是,如果没有一种方便的API方法来获取它,其中一种方法就是遵循以下逻辑:

today -> +1 month -> set day of month to 1 -> -1 day

Of course, that assumes you have date math of that type.

当然,假设你有这种类型的数据。

#10


1  

To get last day of a month in a specific calendar - and in an extension method -:

将一个月的最后一天用一个特定的日历(以及扩展方法)表示:

public static int DaysInMonthBy(this DateTime src, Calendar calendar)
{
    var year = calendar.GetYear(src);                   // year of src in your calendar
    var month = calendar.GetMonth(src);                 // month of src in your calendar
    var lastDay = calendar.GetDaysInMonth(year, month); // days in month means last day of that month in your calendar
    return lastDay;
}

#1


446  

The last day of the month you get like this, which returns 31:

一个月的最后一天,你会得到31:

DateTime.DaysInMonth(1980, 08);

#2


143  

var lastDayOfMonth = DateTime.DaysInMonth(date.Year, date.Month);

#3


66  

DateTime firstOfNextMonth = new DateTime(date.Year, date.Month, 1).AddMonths(1);
DateTime lastOfThisMonth = firstOfNextMonth.AddDays(-1);

#4


23  

If you want the date, given a month and a year, this seems about right:

如果你想要一个月或一年的日期,这似乎是正确的:

public static DateTime GetLastDayOfMonth(this DateTime dateTime)
{
    return new DateTime(dateTime.Year, dateTime.Month, DateTime.DaysInMonth(dateTime.Year, dateTime.Month));
}

#5


9  

Substract a day from the first of next month:

从下个月1日起减除一天:

DateTime lastDay = new DateTime(MyDate.Year,MyDate.Month+1,1).AddDays(-1);

Also, in case you need it to work for December too:

另外,如果你也需要它在12月份工作:

DateTime lastDay = new DateTime(MyDate.Year,MyDate.Month,1).AddMonths(1).AddDays(-1);

#6


8  

You can find the last day of the month by a single line of code:

你可以通过一行代码找到一个月的最后一天:

int maxdt = (new DateTime(dtfrom.Year, dtfrom.Month, 1).AddMonths(1).AddDays(-1)).Day;

#7


6  

You can find the last date of any month by this code:

你可于下列日期找到每个月的最后日期:

var now = DateTime.Now;
var startOfMonth = new DateTime(now.Year, now.Month, 1);
var DaysInMonth = DateTime.DaysInMonth(now.Year, now.Month);
var lastDay = new DateTime(now.Year, now.Month, DaysInMonth);

#8


3  

From DateTimePicker:

从DateTimePicker:

First date:

第一次约会:

DateTime first_date = new DateTime(DateTimePicker.Value.Year, DateTimePicker.Value.Month, 1);

Last date:

最后的日期:

DateTime last_date = new DateTime(DateTimePicker.Value.Year, DateTimePicker.Value.Month, DateTime.DaysInMonth(DateTimePicker.Value.Year, DateTimePicker.Value.Month));

#9


1  

I don't know C# but, if it turns out there's not a convenient API way to get it, one of the ways you can do so is by following the logic:

我不知道c#,但是,如果没有一种方便的API方法来获取它,其中一种方法就是遵循以下逻辑:

today -> +1 month -> set day of month to 1 -> -1 day

Of course, that assumes you have date math of that type.

当然,假设你有这种类型的数据。

#10


1  

To get last day of a month in a specific calendar - and in an extension method -:

将一个月的最后一天用一个特定的日历(以及扩展方法)表示:

public static int DaysInMonthBy(this DateTime src, Calendar calendar)
{
    var year = calendar.GetYear(src);                   // year of src in your calendar
    var month = calendar.GetMonth(src);                 // month of src in your calendar
    var lastDay = calendar.GetDaysInMonth(year, month); // days in month means last day of that month in your calendar
    return lastDay;
}