简体   繁体   English

asp.net中两个日期之间的天数

[英]Days between two dates in asp.net

I find the number of days between the date of the employee's employment and the date of the day, and multiply by the daily amount.我找到雇员受雇日期和当天日期之间的天数,然后乘以每日金额。 The only complaint is that when I find out the number of days between two dates, it calculates over 31 days for the months that draw 31 days naturally.唯一的抱怨是,当我找出两个日期之间的天数时,它计算出自然绘制 31 天的月份超过 31 天。 I need to trade over 30 days while I get the dates between two dates.在获取两个日期之间的日期时,我需要交易超过 30 天。

How can I do that?我该怎么做?

Do you want something like this?你想要这样的东西吗?

DateTime date1 = new DateTime(2016, 10, 3);
DateTime date2 = new DateTime(2016, 11, 3);
var numberOfDays = date2.Subtract(date1).TotalDays;

I Hope this is what you wanted:我希望这是你想要的:

 DateTime firstDay = DateTime.ParseExact("2016-10-03", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
 DateTime lastDate = DateTime.ParseExact("2016-11-03", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
 double daysBetween = (lastDate - firstDay).TotalDays;

如果您只对整月感兴趣,可以使用以下代码

var normalisedDays = ((lastDate.Year - firstDate.Year) * 12 + lastDate.Month - firstDate.Month) * 30;

in Controll :在控制中:

DateTime Date_1 = Date_Start;
DateTime Date_2 = Date_End;
TimeSpan difference = Date_2  - Date_1 ;
var days = difference.TotalDays;

in Script :在脚本中:

<script>
function calculateDifference()
{
    var Date_Start= document.getElementById("Date_Start").value;
    var Date_End= document.getElementById("Date_End").value;
    var Date_StartSplit = Date_Start.split("/");
    var Date_EndSplit = Date_End.split("/");

    var StartDate = new Date(Date_StartSplit[2], Date_StartSplit[0] - 1, Date_StartSplit[1]);
    var EndDate = new Date(Date_EndSplit[2], Date_EndSplit[0] - 1, Date_EndSplit[1]);
    var res = Math.abs(StartDate - EndDate) / 1000;
    var days = Math.floor(res / 86400);
    document.getElementById("Nombre_days").value = days;

}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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