如何从组合框中选择月份名称来获取月份开始日期和月份结束日期
大家好,
我想获取从组合框中选择的当年月份的开始日期和结束日期.
例如:
如果我选择2月,则结果
variable1 ="02/01/2012"
variable2 ="02/28/2012"或"02/29/2012"(如果是leap年)
如何解决该问题,很快就会对我有所帮助.
感谢
hi all,
I want to get start date and end date of the month of current year which I select from combobox.
for example:
If I select feb then the result
variable1="02/01/2012"
variable2="02/28/2012" or "02/29/2012"(if leap year)
how to solve it kindly help me soon.
thanks
这是我们将要使用的DateTime构造函数签名:
Here is the DateTime constructor signature that we are going to use:
public DateTime(
int year,
int month,
int day
);
一个月的第一天
在这里,我们获取日期,并使用DateTime构造函数(年,月,日).我们可以在每月的第一天创建一个新的DateTime对象.
这是获取月份第一天的简单包装方法:
First day of the month
Here we take the date, and using the DateTime constructor (year, month, day). We can create a new DateTime object with the first day of the month.
Here is the simple wrapper method to get the first day of the month:
public DateTime FirstDayOfMonthFromDateTime(DateTime dateTime)
{
return new DateTime(dateTime.Year, dateTime.Month, 1);
}
一个月的最后一天
对于该月的最后一天,我们基本上与该月的第一天做相同的事情,除了在获得该值之后,我们先添加1个月,然后减去1天.瓦拉!我们使用c#来完成每月的最后一天!
这是获取月中最后一天的简单包装方法:
Last day of the month
For the last day of the month we do basically the same thing as the first day of the month, except after we have that value we add 1 month, then subtract 1 day. Walla! We have the last day of the month with c#!
Here is the simple wrapper method to get the last day of the month:
public DateTime LastDayOfMonthFromDateTime(DateTime dateTime)
{
DateTime firstDayOfTheMonth = new DateTime(dateTime.Year, dateTime.Month, 1);
return firstDayOfTheMonth.AddMonths(1).AddDays(-1);
}