码迷,mamicode.com
首页 > Web开发 > 详细

MVC 下 ajax调用 日期差值计算

时间:2015-03-20 12:44:55      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

背景:

服务项目已有服务期起止时间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 (XMLHttpRequest, textStatus) { },
                error: function () { }
            });        // end ajax

        } else {
            $("#ServiceToDate").val("");
            $("#ServiceToDateTitle").val("");
            $("#ServiceToDateH").val(datefromHH);
            $("#ServiceToDateM").val(dateFromMM);
        }

    };

</script>

 

MVC 下 ajax调用 日期差值计算

标签:

原文地址:http://www.cnblogs.com/freeliver54/p/4353150.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!