Javascript 有两个日历日期

Javascript 有两个日历日期,javascript,calendar,Javascript,Calendar,我有两个日历扩展: 一个 “)”onkeypress=“clearInput(“”)”style=“垂直对齐:中间;光标:指针“/> 两个 “)”onkeypress=“clearInput(“”)”style=“垂直对齐:中间;光标:指针”/> 根据这个,我需要计算aghe差异,然后在另一个文本框中以年为单位显示。这是怎么可能的?您可以先将文本框日期转换为datetime对象,如下所示: DateTime myDate = DateTime.ParseExact(YourTextBox

我有两个日历扩展:

一个

“)”onkeypress=“clearInput(“”)”style=“垂直对齐:中间;光标:指针“/>
两个

“)”onkeypress=“clearInput(“”)”style=“垂直对齐:中间;光标:指针”/>

根据这个,我需要计算aghe差异,然后在另一个文本框中以年为单位显示。这是怎么可能的?

您可以先将文本框日期转换为datetime对象,如下所示:

DateTime myDate = DateTime.ParseExact(YourTextBox.Text, "yyyy-MM-dd HH:mm:ss,fff",
                                   System.Globalization.CultureInfo.InvariantCulture)
TimeSpan span = Date1 - Date2;
然后,您应该能够像下面这样计算时间跨度:

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;
}
publicstaticdoubledifferencetotaleyears(此日期时间开始,日期时间结束)
{
//获得总月份的差异。
整月=((年末-年初)*12)+(月末-月初);
//如果月末未完成,则减去1个月
如果(结束日<开始日)
{
月--;
}
双倍总年=月/12d;
回归年数;
}