Normally I use the below code, but is there a better way?
通常我使用下面的代码,但是有更好的方法吗?
lastOfMonth = new DateTime(Now.Year, Now.Month, 1).AddMonths(1).AddDays(-1)
5 个解决方案
#1
50
I use
我使用
DateTime now = DateTime.Today;
var lastDate = new DateTime(now.Year, now.Month, DateTime.DaysInMonth(now.Year, now.Month));
#2
12
I would probably use DaysInMonth
as it makes the code a bit more readable and easier to understand (although, I really like your trick :-)). This requieres a similar ammount of typing (which is quite a lot), so I would probably define an extension method:
我可能会使用DaysInMonth,因为它使代码更容易读懂(虽然,我真的很喜欢你的技巧:-)。这需要类似的输入数量(相当多),所以我可能会定义一个扩展方法:
DateTime LastDayOfMonth(this DateTime) {
var days = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
return new DateTime(DateTime.Now.Year, DateTime.Now.Month, days);
}
Now we can use DateTime.Now.LastDayOfMonth()
which looks a lot better :-).
现在我们可以用DateTime.Now.LastDayOfMonth(),看起来好多了。
#3
8
DateTime(year, month, DateTime.DaysInMonth(year, month)).
#4
4
You can use CultureInfo.CurrentCulture.Calendar.GetDaysInMonth(Now.Year, Now.Month)
你现在可以使用CultureInfo.CurrentCulture.Calendar.GetDaysInMonth(。年,Now.Month)
#5
2
Here is how you can get the number of days in the month using Noda Time:
下面是如何利用野田时间计算一个月的天数:
int days = CalendarSystem.Iso.GetDaysInMonth(year, month);
Pretty simple, eh? Well that assumes you know the year and month you are asking about. If you want it for the current month, and in the system's time zone, then you have to specify that explicitly, like this:
很简单,不是吗?假设你知道你要问的年份和月份。如果你想要这个月,在系统的时区,那么你必须明确地指定它,像这样:
DateTimeZone tz = DateTimeZoneProviders.Tzdb.GetSystemDefault();
LocalDate localDate = SystemClock.Instance.Now.InZone(tz).Date;
int days = localDate.Calendar.GetDaysInMonth(localDate.Year, localDate.Month);
Noda Time intentionally makes you think about these things, instead of just making the assumptions that DateTime.Now
or DateTime.Today
do.
野田时间故意让你思考这些事情,而不是仅仅假设时间。现在还是DateTime。今天做的事。
#1
50
I use
我使用
DateTime now = DateTime.Today;
var lastDate = new DateTime(now.Year, now.Month, DateTime.DaysInMonth(now.Year, now.Month));
#2
12
I would probably use DaysInMonth
as it makes the code a bit more readable and easier to understand (although, I really like your trick :-)). This requieres a similar ammount of typing (which is quite a lot), so I would probably define an extension method:
我可能会使用DaysInMonth,因为它使代码更容易读懂(虽然,我真的很喜欢你的技巧:-)。这需要类似的输入数量(相当多),所以我可能会定义一个扩展方法:
DateTime LastDayOfMonth(this DateTime) {
var days = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
return new DateTime(DateTime.Now.Year, DateTime.Now.Month, days);
}
Now we can use DateTime.Now.LastDayOfMonth()
which looks a lot better :-).
现在我们可以用DateTime.Now.LastDayOfMonth(),看起来好多了。
#3
8
DateTime(year, month, DateTime.DaysInMonth(year, month)).
#4
4
You can use CultureInfo.CurrentCulture.Calendar.GetDaysInMonth(Now.Year, Now.Month)
你现在可以使用CultureInfo.CurrentCulture.Calendar.GetDaysInMonth(。年,Now.Month)
#5
2
Here is how you can get the number of days in the month using Noda Time:
下面是如何利用野田时间计算一个月的天数:
int days = CalendarSystem.Iso.GetDaysInMonth(year, month);
Pretty simple, eh? Well that assumes you know the year and month you are asking about. If you want it for the current month, and in the system's time zone, then you have to specify that explicitly, like this:
很简单,不是吗?假设你知道你要问的年份和月份。如果你想要这个月,在系统的时区,那么你必须明确地指定它,像这样:
DateTimeZone tz = DateTimeZoneProviders.Tzdb.GetSystemDefault();
LocalDate localDate = SystemClock.Instance.Now.InZone(tz).Date;
int days = localDate.Calendar.GetDaysInMonth(localDate.Year, localDate.Month);
Noda Time intentionally makes you think about these things, instead of just making the assumptions that DateTime.Now
or DateTime.Today
do.
野田时间故意让你思考这些事情,而不是仅仅假设时间。现在还是DateTime。今天做的事。