获取一个月的天数 [英] Getting number of days in a month
本文介绍了获取一个月的天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要知道的是该月份的天数。
var month = cmbMonth.SelectedIndex + 1;
DateTime date = Convert.ToDateTime(month);
所以如果我用户选择1月,我需要保存31到一个变量。谢谢。
解决方案
你想要 DateTime.DaysInMonth
:
int days = DateTime.DaysInMonth(year,month);
显然,它随着年份而变化,因为有时候二月有28天,有时候是29.你可以随时选择一个特别的一年(跳跃或不跳跃)如果你想修复它的一个价值或其他。
I have a comboBox with all the months in it.
What I need to know is the number of days in that month.
var month = cmbMonth.SelectedIndex + 1;
DateTime date = Convert.ToDateTime(month);
So if I user selects January, I need to save 31 to a variable. Thanks.
解决方案
You want DateTime.DaysInMonth
:
int days = DateTime.DaysInMonth(year, month);
Obviously it varies by year, as sometimes February has 28 days and sometimes 29. You could always pick a particular year (leap or not) if you want to "fix" it to one value or other.
这篇关于获取一个月的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文