简体   繁体   English

计算给定开始日期和结束日期之间的天数

[英]Count days between given Start date and End date

I wants to count the number of days between given start date and end date. 我想计算给定开始日期和结束日期之间的天数。

where start date and end date will entered by the user. 用户将在其中输入开始日期和结束日期。 using that i have to count total days between them. 使用它,我必须计算它们之间的总天数。

my database is as below 我的数据库如下

leave_id int leave_start_date DateTime leave_end_date DateTime leave_days int Leave_id int Leave_start_date DateTime Leave_end_date DateTime Leave_days int

and my .cs(class) files of Model are as below 和我的.cs(class)文件的模型如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

namespace LeaveModule.Models
{
    public class LeaveSet
    {
        public int user_id { get; set; }
        public DateTime leave_start_date { get; set; }
        public DateTime leave_end_date { get; set; }
        public string leave_description { get; set; }
        public decimal leave_days { get; set; } 
    }
}

2nd file 第二档

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ProjectManagementSystem.Models;

namespace LeaveModule.Models
{
    public class LeaveSetFetch
    {

        public static IList<LeaveSet> all()
        {
            IList<LeaveSet> result =
                (IList<LeaveSet>)HttpContext.Current.Session["leave"];

            if (result == null)
            {
                HttpContext.Current.Session["leave"] = result =
                    (from l in new ProjectManagementSystemEntities3().leave_master
                     select new LeaveSet
                     {
                         user_id = l.user_id,
                         leave_start_date = l.leave_start_date,
                         leave_end_date = l.leave_end_date,
                         leave_description = l.leave_description,
                         leave_days = l.leave_days,
                         //   is_valid = l.is_valid,

                     }).ToList();

            }
            return result;
        }
    }
}

So how can i do that in MVC3 ? 那么我该如何在MVC3中做到这一点呢?

Thanks in advance.. 提前致谢..

DateTime dt1 = new DateTime(2012, 3, 3);
DateTime dt2 = new DateTime(2011, 3, 3);
var days = dt1.Subtract(dt2).TotalDays;

Bonus: Here is a good blog from Jon Skeet about .Net's DateTime class 奖励: 是乔恩·斯基特(Jon Skeet)撰写的有关.Net的DateTime类的好博客

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

相关问题 计算结束日期给定开始日期和持续时间(以天为单位) - Calculate End Date Given Start Date & duration (in days) 给定开始日期,如何根据the年查找365天或366天的结束日期 - given a start date how to find the end date which have 365 or 366 days based on the leap year 如何查找从给定日期和日期(不包括星期日)开始的结束日期 - How to find End date from the given date and days excluding sundays 两个日期范围之间相交的天数 - Count of days intersecting between two date ranges 获取给定开始日期,结束日期和星期几的日期 - Get dates given a start date an end date and day of week 给定结束日期和返回日期时计算开始日期 - Calculate StartDate when End Date & back days given 获取特定月份的开始日期和结束日期 - Get Start Date and End date for certain month count 如何在SSRS中获取开始日期和结束日期之间的差的平均值 - How to get the average of the difference between start date and end date in SSRS 如何遍历一类开始和结束日期时间,将每个日期范围之间的天数与每次迭代一起存储到列表中? - How do you loop through a class of start and end DateTimes, storing the days between each date range to a list, with each iteration? 获取有关给定开始日期和结束日期的季度数据 - To Get Quarterly data with respect to a given Start and end date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM