简体   繁体   中英

have two calendar extendar dates

I have two calendar extendars:

One

<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>

Two

<asp:TextBox ID="txtStartDate" runat="server" Text="" style="padding-left:8px;width:136px;" />&nbsp;
<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>  

From this I need to calculate the aghe difference and then show that in years in another text box. How is this possible?

You can convert the textbox dates to datetime object first like below:

DateTime myDate = DateTime.ParseExact(YourTextBox.Text, "yyyy-MM-dd HH:mm:ss,fff",
                                   System.Globalization.CultureInfo.InvariantCulture)

Then you should be able to calculate timespan like below:

TimeSpan span = Date1 - Date2;

Then to convert in years.

int years = (span).Year

Or you can create function to be generic also.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM