2012-12-18 185 views
3

我在AjaxControlToolkit中使用了Calender Extender Control。基本上有兩種控制日期:Start DateEnd date(均與日曆擴展器相關)。根據選擇的開始日期,我在結束日期字段中填充日期,例如添加月份或日期的數量。但就像我已經能夠增加數月,但也想設定一個我無法做的那個月的特定日子。在日期中設置特定日期

例如: 今天的日期是18 Dec 2012。就像每三個月中的第一個,所以我增加3個月,這個月出來是Feb 2013。但我想設置日期1st Feb 2013。我無法做到。請幫助。

+0

We你怎麼樣做,你添加月份的日期將有不同的一天! ! – V4Vendetta

+0

@ V4Vendetta:用於添加月數我正在使用像 DueDate.Text =(DateTime.Parse(StartDate.Text).AddMonths(N))。ToShortDateString(); 因此,我能夠添加月份,但無法設置月的特定日期 – Rahul2788

+0

那麼試着使用'new DateTime(yr,mnt,yourday).ToShortDateString()',這裏yr會在startdate和月爲+ N,然後設定你的具體日期 – V4Vendetta

回答

6

您可以通過添加月份來設置月份中的某一天。

DateTime todayDate = DateTime.Now; 
DateTime after3MonthDate = todayDate.AddMonths(3); 
//Set First Day of Month 
after3MonthDate = new DateTime(after3MonthDate.Year, after3MonthDate.Month, 1); 
+0

完成!謝謝拍品 – Rahul2788

1

試試這個:

// 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); 
} 

// Set the due date... 
DueDate.Text = (FirstDayOfMonthFromDateTime(DateTime.Parse(StartDate.Text).AddMonths(N))).ToShortDateString(); 

您還可以修改包裝方法來獲得一個月的任何一天:

public DateTime DayOfMonthFromDateTime(DateTime dateTime, int day) 
{ 
    return new DateTime(dateTime.Year, dateTime.Month, day); 
} 
+0

完成!謝謝拍品 – Rahul2788

0

該代碼可用於現有的日期時間變量將當天部分設置爲月份的第一天:

if(myDate.Day > 1) 
{ 
    myDate = myDate.AddDays(-(myDate.Day - 1)); 
} 
+0

我可以請求您請您在答案上添加更多背景。僅有代碼的答案很難理解。如果您可以在帖子中添加更多信息,它可以幫助提問者和未來的讀者。 – RBT