我有两个日历延期:
<asp:TextBox ID="txtDOB" runat="server" Text="" />
<asp:CalendarExtender ID="calDOB" runat="server" TargetControlID="txtDOB" PopupButtonID="imgCalendarDOB" />
<img src="images/calendar_rbc.gif" class="calenderImg" id="imgDOB" alt="" style="vertical-align: middle;cursor: pointer" />
<img src="images/clear.png" class="calenderClearImg" alt=" " tabindex="0" id="clearDOB onclick="clearInput('<%=txtDOB.ClientID %>')" onkeypress="clearInput('<%=txtDOB.ClientID %>')" style="vertical-align: middle; cursor: pointer" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="txtDOB" ErrorMessage="*" ID="rdqDate"></asp:RequiredFieldValidator>
<asp:TextBox ID="txtStartDate" runat="server" Text="" style="padding-left:8px;width:136px;" />
<asp:CalendarExtender ID="calStartDate" runat="server" TargetControlID="txtStartDate" PopupButtonID="imgCalendarStartDate" />
<img src="images/calendar_rbc.gif" class="calenderImg" id="imgStartDate" alt="" style="vertical-align: middle;cursor: pointer" />
<img src="images/clear.png" class="calenderClearImg" alt=" " tabindex="0" id="clearStartDate" onclick="clearInput('<%=txtStartDate.ClientID %>')" onkeypress="clearInput('<%=txtStartDate.ClientID %>')" style="vertical-align: middle;cursor: pointer" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="txtStartDate" ErrorMessage="*" ID="RequiredFieldValidator1"></asp:RequiredFieldValidator>
由此我需要计算aghe差异,然后在另一个文本框中显示年份。这怎么可能?
答案 0 :(得分:0)
您可以首先将文本框日期转换为datetime对象,如下所示:
DateTime myDate = DateTime.ParseExact(YourTextBox.Text, "yyyy-MM-dd HH:mm:ss,fff",
System.Globalization.CultureInfo.InvariantCulture)
然后你应该能够计算如下的时间跨度:
TimeSpan span = Date1 - Date2;
然后转换多年。
int years = (span).Year
或者您也可以创建通用功能。
public static double DifferenceTotalYears(this DateTime start, DateTime end)
{
// Get difference in total months.
int months = ((end.Year - start.Year) * 12) + (end.Month - start.Month);
// substract 1 month if end month is not completed
if (end.Day < start.Day)
{
months--;
}
double totalyears = months / 12d;
return totalyears;
}