c# .net

How to calculate days difference between two dates in c#?

How to calculate days difference between two dates in c#?, someone asked me to explain?
In this tutorial I will show you how to calculate days difference between two days using asp.net mvc c#. Here, I m using jQuery ui datepicker, calculate button and to display the result in a div element. Initialize the datepicker format in script, when the button clicked, calling the controller method from jquery ajax.
View:
<script type="text/javascript">
    $(document).ready(function () {
        $("#dtpStartDate").datepicker({
            dateFormat: 'dd-mm-yy'
        });
 
        $("#btnCalculate").click(function () {
            var date = $('#dtpStartDate');
            var data = { date: date.val() };
            var json = JSON.stringify(data);
            $.ajax({
                url: '/DateDiff/GetDetails',
                type: "POST",
                data: json,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (result) {
                    $("#divHtml").html(result.dateDiff);
                    return false;
                },
                error: function (err) {
                    alert(err.statusText);
                }
            });
            return false;
        });
    });
 
</script>
Controller:
[HttpPost]
        public JsonResult GetDetails(string date)
        {
            CultureInfo culture = new CultureInfo("es-ES");
            DateTime prevDate = DateTime.Parse(date, culture);
 
            var todayDate = DateTime.Now;
            var dateDiff = (todayDate - prevDate).Days;
 
            var json = Json(new { dateDiff });
            return json;
        }

difference between two dates in c#
Video Guide:

Post your comments / questions