예제 #1
0
        public void TestGetDateDiffInDays()
        {
            Date startDate1 = new Date(27, 12, 2015);
            Date endDate1   = new Date(27, 12, 2016);

            Date startDate2 = new Date(7, 3, 2015);
            Date endDate2   = new Date(27, 12, 2017);

            Date startDate3 = new Date(27, 2, 2015);
            Date endDate3   = new Date(27, 3, 2017);

            Date startDate4 = new Date(27, 3, 2015);
            Date endDate4   = new Date(7, 12, 2017);

            Date startDate5 = new Date(27, 3, 2015);
            Date endDate5   = new Date(7, 12, 2020);

            Date startDate6 = new Date(27, 3, 2015);
            Date endDate6   = new Date(11, 1, 2016);



            Assert.AreEqual(366, DateService.GetDateDiffInDays(startDate1, endDate1));
            Assert.AreEqual(1026, DateService.GetDateDiffInDays(startDate2, endDate2));
            Assert.AreEqual(759, DateService.GetDateDiffInDays(startDate3, endDate3));
            Assert.AreEqual(986, DateService.GetDateDiffInDays(startDate4, endDate4));
            Assert.AreEqual(2082, DateService.GetDateDiffInDays(startDate5, endDate5));
            Assert.AreEqual(290, DateService.GetDateDiffInDays(startDate6, endDate6));
        }
        public ActionResult CalculateDateDiff(String startDate, String endDate)
        {
            String dateDiff;

            if (String.Compare(startDate, endDate) > 0)
            {
                dateDiff = "Start date should always be before end date!";
            }
            else
            {
                String[] startDateSplit = startDate.Split('-');
                Date     startDateObj   = new Date(int.Parse(startDateSplit[2]), int.Parse(startDateSplit[1]), int.Parse(startDateSplit[0]));

                String[] endDateSplit = endDate.Split('-');
                Date     endDateObj   = new Date(int.Parse(endDateSplit[2]), int.Parse(endDateSplit[1]), int.Parse(endDateSplit[0]));

                int dateDiffResult = DateService.GetDateDiffInDays(startDateObj, endDateObj);
                dateDiff = String.Format("Days between two dates: {0}", dateDiffResult);
            }

            return(Json(dateDiff, JsonRequestBehavior.AllowGet));
        }