你的位置:首页 > ASP.net教程

[ASP.net教程]MVC 下 ajax调用 日期差值计算


背景:

服务项目已有服务期起止时间From-To

现在要根据用户输入的新的起始时间, 和该服务期的原有区间值, 计算出新的服务期截止时间

即 NewServiceToDateTime = NewServiceFromDateTime + ( OldServiceToDateTime - OldServiceFromDateTime)

 

本示例的效果:

1.日期转换 及 差值计算

2.mvc 下 ajax 调用及赋值

 

相关代码如下:

后台Controller下Action处理

public DateTime GetDateTimeParseStr(string strDate,string strDateH,string strDateM) {      DateTime dt = new DateTime();      if (strDate.Contains("/"))      {        dt = DateTime.ParseExact(strDate, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);      }      else if (strDate.Contains("-"))      {        dt = DateTime.ParseExact(strDate, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);      }      else      {        dt = DateTime.Parse(strDate);      }      if (!string.IsNullOrEmpty(strDateH)) {        dt = dt.AddHours(int.Parse(strDateH));      }      if (!string.IsNullOrEmpty(strDateM))      {        dt = dt.AddMinutes(int.Parse(strDateM));      }            return dt;    }    public DateTime GetNewToDateTime(DateTime from, DateTime to, DateTime newFrom)    {      TimeSpan tsFrom = new TimeSpan(from.Ticks);      TimeSpan tsTo = new TimeSpan(to.Ticks);      TimeSpan tsNewFrom = new TimeSpan(newFrom.Ticks);      DateTime dtNewTo = newFrom.AddTicks(tsTo.Subtract(tsFrom).Ticks);      return dtNewTo;    }    public JsonResult GetNewToDateTimeJson(string strFrom,string strFromH,string strFromM      ,string strTo,string strToH ,string strToM,      string strFromNew, string strFromHNew, string strFromMNew)    {      try      {        DateTime from = GetDateTimeParseStr(strFrom, strFromH,strFromM);        DateTime to = GetDateTimeParseStr(strTo, strToH, strToM);        DateTime newFrom = GetDateTimeParseStr(strFromNew, strFromHNew, strFromMNew);         DateTime dtNewTo = GetNewToDateTime(from, to, newFrom);        return Json(new        {          Success = true          ,          ServiceToDateTitle = dtNewTo.FormatDateAndTimeNoSecond()          ,          ServiceToDate = dtNewTo.ToString("yyyy-MM-dd")          ,          ServiceToDateH = dtNewTo.Hour.ToString().PadLeft(2, '0')          ,          ServiceToDateM = dtNewTo.Minute.ToString().PadLeft(2, '0')        }, JsonRequestBehavior.AllowGet);      }      catch (Exception)      {        return Json(new        {          Success = false        }, JsonRequestBehavior.AllowGet);      }          }

前台aspx的Ajax调用

<script type="text/javascript">  function changeServicePeriod() {    var dateFrom = $("#ServiceFromDate").val();    var datefromHH = $("#ServiceFromDateH").val();    var dateFromMM = $("#ServiceFromDateM").val();    if (dateFrom != "") {      var item = eval("(" + $("#itemdata").val() + ")");      var dateFromOld = item.ServiceFromDate;      var dateFromHHOld = item.ServiceFromDateH;      var dateFromMMOld = item.ServiceFromDateM;      var dateToOld = item.ServiceToDate;      var dateToHHOld = item.ServiceToDateH;      var dateToMMOld = item.ServiceToDateM;      var url = '<%=Url.Action("GetNewToDateTimeJson", "ServiceItem") %>';      $.ajax({        type: "get",        async: false,        url: url,        data: {          strFrom: dateFromOld         , strFromH: dateFromHHOld         , strFromM: dateFromMMOld         , strTo: dateToOld         , strToH: dateToHHOld         , strToM: dateToMMOld         , strFromNew: dateFrom         , strFromHNew: datefromHH         , strFromMNew: dateFromMM        },        dataType: "json",        success: function (jsonData, textStatus) {          if (jsonData.Success) {            $("#ServiceToDateTitle").val(jsonData.ServiceToDateTitle);            $("#ServiceToDate").val(jsonData.ServiceToDate);            $("#ServiceToDateH").val(jsonData.ServiceToDateH);            $("#ServiceToDateM").val(jsonData.ServiceToDateM);          } else {            $("#ServiceToDate").val("");            $("#ServiceToDateTitle").val("");            $("#ServiceToDateH").val(datefromHH);            $("#ServiceToDateM").val(dateFromMM);          }        },        complete: function (function () { }      });    // end ajax    } else {      $("#ServiceToDate").val("");      $("#ServiceToDateTitle").val("");      $("#ServiceToDateH").val(datefromHH);      $("#ServiceToDateM").val(dateFromMM);    }  };</script>