I have page in want to calculate hire date till now date in years, months and days textbox1 textbox2 textbox3
if month more than 12 then add on years also for days
According to your description, I made demo for your.
You can judge the number of days and months of the date separately. If the previous value is less than the latter value,
then the corresponding month and year are decremented by one.
The code:
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" />
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$(document).ready(function () {
$("#TextBox1").datepicker({
});
$("#TextBox2").datepicker({
onSelect: function () {
myfunc();
}
});
function diff_months(dt1, dt2) {
var d1 = dt1, d2 = dt2;
if (dt1 < dt2) {
d1 = dt2;
d2 = dt1;
}
var m = (d1.getFullYear() - d2.getFullYear()) * 12 + (d1.getMonth() - d2.getMonth());
if (d1.getDate() < d2.getDate()) {
--m;
};
return m;
}
function diff_years(year1, year2) {
var y1 = year1, y1 = year2;
if (year1 < year2) {
y1 = year2;
y2 = year1;
}
var y = (y1.getFullYear() - y2.getFullYear());
if (y1.getMonth() < y2.getMonth()) {
--y;
};
return y;
}
function myfunc() {
var start = $("#TextBox1").datepicker("getDate");
var end = $("#TextBox2").datepicker("getDate");
days = (end - start) / (1000 * 60 * 60 * 24);
$("#TextBox3").val(days + 1);
$("#TextBox4").val(diff_months(start, end));
$("#TextBox5").val(diff_years(start, end));
}
});
</script>
hire date:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
Day:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
Month:<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />
Year:<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox><br />
The result:
Best regards,
Sam
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
5 Points
110 Posts
hiredate calculating
Nov 05, 2019 11:27 AM|thepast|LINK
hi
I have page in want to calculate hire date till now date in years, months and days textbox1 textbox2 textbox3
if month more than 12 then add on years also for days
thank you
All-Star
43841 Points
18769 Posts
Re: hiredate calculating
Nov 05, 2019 01:27 PM|mgebhard|LINK
Just subtract the hire date from Today. The substation results in a TimeSpan type which has has years, months, days, everything you need.
TimeSpan reference documentation.
https://docs.microsoft.com/en-us/dotnet/api/system.timespan?view=netframework-4.8
If you are having trouble writing logic, then share your code and explain the expected results and the actual results.
Participant
1890 Points
731 Posts
Re: hiredate calculating
Nov 06, 2019 08:17 AM|samwu|LINK
Hi thepast,
According to your description, I made demo for your.
You can judge the number of days and months of the date separately. If the previous value is less than the latter value,
then the corresponding month and year are decremented by one.
The code:
The result:
Best regards,
Sam
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.