DATEDIFF how to write in Asp.net MVC

c# datediff days
c# datediff months
how to calculate difference between two dates in asp.net using c#
c# datediff in minutes
c# datediff years
vb.net datediff months
datediff vb.net example
get difference between two dates c# in days

I am creating a car retail system. I need to the display if I enter the correct carid relevant car information will be displayed in the below text boxes. I have attached the screen shot image below. I need to calculate day difference between start date and end date for calculation of how many days between because retail fee needs to be calculated for extra days of the retail system. In C# i wrote like this select car_id,cust_id,due,DATEDIFF(GETDATE(),due) as elap from rental where car_id = ? I don't know how to write in Asp.net MVC.

Code I tried:

form design

<div class="row">
    <div class="col-sm-8">

        @using (Html.BeginForm("Save", "return", FormMethod.Post, new { id = "popupForm" }))
        {
            <div>
                <h4> Registation</h4>
            </div>

              <div class="card-action">

                <label class="form-label">Car ID</label>
                  <input type="text" id="carno" name="carno" class="form-control" placeholder="carno" required />
            </div>



            <div class="card-action">

                <label class="form-label">Customer ID</label>

                <input type="text" id="custid" name="custid" class="form-control" placeholder="Customer ID" required />

            </div>



            <div class="card-action">

                <label class="form-label">Date</label>

                <input type="text" id="date" name="date" class="form-control" placeholder="Date" required />

            </div>


             <div class="card-action">

                <label class="form-label">Days Elapsed</label>

                 <input type="text" id="elap" name="elap" class="form-control" placeholder="Date" required />

            </div>


             <div class="card-action">

                <label class="form-label">Fine</label>

                 <input type="text" id="fine" name="fine" class="form-control" placeholder="Fine" required />

            </div>


            <div class="card">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>

        }
    </div>

</div>

jQuery seach the data using jquery

   <script>
        getProductcode();
        function getProductcode() {
            $("#carno").empty();
            $("#carno").keyup(function (e)
            {
                var q = $("#carno").val();
                $.ajax({
                    type: "POST",
                    url: '/return/Getid?carno=' + $("#carno").val(),
                    dataType: "JSON",
                    success: function (data)
                    {
                        console.log(data);
                          $('#custid').val(data.custid);
                          $('#date').val(data.sdate);
                           $('#elap').val(data.elap);

                    },
                    error: function (xhr, status, error)
                    {
                        //  alert("The barcode entered is not correct");
                    }
                });
                return true;
            });
        }

    </script>

returnController

 [HttpPost]
        public ActionResult Getid(String carno)
        {
            carrentEntities1 db = new carrentEntities1();
            var carn = (from s in db.rentails where s.carno == carno select s.custid).ToList();
            return Json(carn, JsonRequestBehavior.AllowGet);
        }

I don't know how to write the Datediff to calculate days. In this no result displayed. I tested custid, it didn't display.

Database fields

id  carno   custid  fee   sdate      edate
1   1        1     1200  2019-12-09  2019-12-19
2   1        1     20000 2019-12-01  2019-12-31
3   A0001    1     3434  2019-12-09  2019-12-27

You can use SqlFunctions.DateDiff like this :

   var carn = (from s in db.rentails where s.carno == carno 
       select new {   
       StartDate = s.sdate,
       EndDate = s.edate,
       CarNo = s.carno,
       Fee = s.fee,
       ElapsedDays = SqlFunctions.DateDiff("day",DateTime.Now,s.sdate)
    }).ToArray();

Document.

DateAndTime.DateDiff Method (Microsoft.VisualBasic), Hi! I'm a C# novice moving from VB. In VB, I can easily use DateDiff function. For example, Dim x As Integer = DateDiff(DateInterval.Day, 10 / 1� ASP.NET. ASP.NET was released in 2002 as a successor to Classic ASP. ASP.NET pages have the extension .aspx and are normally written in C# (C sharp). ASP.NET 4.6 is the latest official version of ASP.NET. ASP.NET 5 was expected to be an important redesign of ASP.NET. However, the development of ASP.NET 5 was stopped in favor of ASP.NET Core.

In C# you don't need to write Datediff() function like in SQL. You just can minus needed dates:

var carn = (from s in db.rentails where s.carno == carno select s).
.ToList()
.Select(x => new 
{
DateDiff = (x.edate - x.sdate).Days,
//rest props
});

DateDiff function in C#, using DateTime.Substract method, Subtraction Operator, and DateDiff Function . Code Sample: Calculating the Date Difference - Subtract Method. using System;; using Net DateTime best practices. Brought to you by:. Dim wD As Long = DateDiff(DateInterval.Weekday, datTim1, datTim2) Dim wY As Long = DateDiff(DateInterval.WeekOfYear, datTim1, datTim2) In the preceding example, DateDiff returns 0 to wD because the difference between the two dates is less than seven days, but it returns 1 to wY because there is a seven-day difference between the first days of

Pls give it a try the following code:

var carn = (from s in db.rentails where s.carno == carno select s)
.Select(x => new 
{
  DateDiff = (x.edate - x.sdate).Days
});

Note: Make sure type of eDate, sDate is DateTime.

Calculating Date Difference in C#, I have the below line of code in VB DateDiff(DateInterval.Day, Date. DateDiff function in C#. Can anybody help me out how to write this in C# I need to calculate day difference between start date and end date for calculation of how many days between because retail fee needs to be calculated for extra days of the retail system. In C# i wrote like this select car_id,cust_id,due,DATEDIFF(GETDATE(),due) as elap from rental where car_id = ? I don't know how to write in Asp.net MVC.

How to use DateDIff function in C#, If you are looking for a DateDiff function in C# like in VB or SQL Server, there is none. However Net via C#. Write you own DateDiff function. In this chapter, we will discuss about building models in an ASP.NET MVC Framework application. A model stores data that is retrieved according to the commands from the Controller and displayed in the View. Model is a collection of classes wherein you will be working with data and business logic

DateDiff Equivalent in C# – 3 Options, DatePart, DateAdd and DateDiff functions in SQL Server - Part 27 Write a query to compute the age of a person, when the date of birth is given. The output� In this article, I will explain how to build a simple user registration form that will allow users to register using ASP.NET MVC. Let's understand the MVC architecture in ASP.NET MVC stands for Model, View and Controller. MVC separates application into three components - Model, View and Controller. Model

DatePart, DateAdd and , You should give an example of two dates and tell how you want the output. Because, its not clear if it is 15 days of difference between two dates� Finding out difference in two dates by using DateDiff function We can find out the difference between two dates by using DateDiff function of VBScript used in ASP. DateDiff() finds out the difference of the dates based on the required date part like month, year, days etc. Here is the basic syntax of DateDiff function.

Comments
  • var carn = (from s in db.rentails where s.carno == carno select s.custid).Select(t=> s.sdate { ElapsedDays = SqlFunctions.DateDiff("day",DateTime.Now,t.edate) }).ToArray(); error displayed
  • @Javafiver i forgot to put new keyword. And your syntax is not correct. See anonymous types
  • var carn = (from s in db.rentails where s.carno == carno select s.custid).Select(t=> new { // here how to select start date ElapsedDays = SqlFunctions.DateDiff("day",DateTime.Now,t.sdate) }).ToArray();
  • start sdate enddate edate this database table colums
  • @Javafiver made a valid select statement.
  • DateDiff = (x.edate - x.sdate).Days, this line x.edate - x.sdate getting error sir
  • var carn = (from s in db.rentails where s.carno == carno select s). this error indicate error
  • DateDiff = (x.edate - x.sdate).Days in this line Days error
  • Maybe you have not used namespace for that ? Becuz it works fine for me.
  • Days what is namespaces to add