我想计算给定开始日期和结束日期之间的天数。
用户输入开始日期和结束日期。 使用它我必须计算它们之间的总天数。
我的数据库如下
leave_id int leave_start_date DateTime leave_end_date日期时间 leave_days int
和我的.cs(类)文件如下所示
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; }
}
}
第二档
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;
}
}
}
那么我怎样才能在 MVC3 中做到这一点?
提前致谢..
答案 0 :(得分:4)
DateTime dt1 = new DateTime(2012, 3, 3);
DateTime dt2 = new DateTime(2011, 3, 3);
var days = dt1.Subtract(dt2).TotalDays;
Bonus:Here是Jon Skeet关于.Net的DateTime课程的好博客