Calculate Date difference using jQuery Ajax in ASP.Net Core MVC

trisetia302
 
on Aug 19, 2021 10:46 AM
Sample_153670.zip
1150 Views

Hi Guys,

I'm trying to make 2 function in controller for calculation book return.

1. function for search calculation book return.

2. function for search interval day beetwen 2 dates.

and then I make 1 action method to running 2 function above inside the controller, but all this 3 textbox just return zero value eventhough it's too late to return the book.

Any help could be apriciate.

<input asp-for="Denda" id="Denda" class="form-control" readonly="readonly" />
<input asp-for="Jumlah_Hari_Terlambat" id="Jumlah_Hari_Terlambat" class="form-control" readonly="readonly" />
<input asp-for="Total_Denda" id="Total_Denda" class="form-control" readonly="readonly" />

Model.cs

    public class Pengembalian
    {
        [Key]
        [Display(Name = "Kode Kembali")]
        [Required(ErrorMessage = "Kode Kembali is required.")]
        public string Kode_Kembali { get; set; }

        [Display(Name = "Tanggal Pinjam")]
        [Required(ErrorMessage = "Tanggal Pinjam is required.")]
        public string Tanggal_Pinjam { get; set; }

        [Display(Name = "Tanggal Kembali")]
        [Required(ErrorMessage = "Tanggal Kembali is required.")]
        public string Tanggal_Kembali { get; set; }

        [Display(Name = "Tanggal Jatuh Tempo")]
        [Required(ErrorMessage = "Tanggal Jatuh Tempo is required.")]
        public string Tanggal_Jatuh_Tempo { get; set; }

        [Display(Name = "Denda")]
        [Required(ErrorMessage = "Denda is required.")]
        public string Denda { get; set; }

        [Display(Name = "Jumlah Hari Terlambat")]
        [Required(ErrorMessage = "Jumlah Hari Terlambat is required.")]
        public string Jumlah_Hari_Terlambat { get; set; }


        [Display(Name = "Total Denda")]
        [Required(ErrorMessage = "Total Denda is required.")]
        public string Total_Denda { get; set; }


        [Display(Name = "Kode Petugas")]
        [Required(ErrorMessage = "Kode Petugas is required.")]
        public string Kode_Petugas { get; set; }

        [Display(Name = "Nama Petugas")]
        [Required(ErrorMessage = "Nama Petugas is required.")]
        public string Nama_Petugas { get; set; }

        [Display(Name = "Kode Anggota")]
        [Required(ErrorMessage = "Kode Anggota is required.")]
        public string Kode_Anggota { get; set; }

        [Display(Name = "Nama Anggota")]
        [Required(ErrorMessage = "Kode Anggota is required.")]
        public string Nama_Anggota { get; set; }

        [Display(Name = "Kode Pinjam")]
        [Required(ErrorMessage = "Kode Pinjam is required.")]
        public string Kode_Pinjam { get; set; }
    }

Controller.cs

        [HttpPost]
        public JsonResult Hitung(Pengembalian pengembalianBuku)
        {
            CariSelisihHariTerlambatBalikinBuku(pengembalianBuku);
            CariDenda(pengembalianBuku);
            return Json(pengembalianBuku);
        }

        //function for search calculation book return fine
        private void CariDenda(Pengembalian pengembalianBuku)
        {
            DateTime Tanggal_Kembali = Convert.ToDateTime(pengembalianBuku.Tanggal_Kembali);
            DateTime TglJatuhTempo = Convert.ToDateTime(pengembalianBuku.Tanggal_Jatuh_Tempo);

            if (Tanggal_Kembali <= TglJatuhTempo)
            {
                int Denda = 0;
                pengembalianBuku.Denda = Convert.ToString(Denda);
            }
            else if (Tanggal_Kembali > TglJatuhTempo)
            {
                decimal Denda = 2000;
                decimal JmlHariTerlambat = Convert.ToDecimal(pengembalianBuku.Jumlah_Hari_Terlambat);
                Denda = Convert.ToDecimal(pengembalianBuku.Denda);
                decimal Hasil = JmlHariTerlambat * Denda;
                pengembalianBuku.Total_Denda = Convert.ToString(string.Format("{0:0,00.00}", Hasil));
            }
        }

        //Function for search interval days beetwen 2 dates.
        private void CariSelisihHariTerlambatBalikinBuku(Pengembalian pengembalianBuku)
        {
            DateTime TglJatuhTempo = Convert.ToDateTime(pengembalianBuku.Tanggal_Jatuh_Tempo);
            DateTime TglPengembalian = Convert.ToDateTime(pengembalianBuku.Tanggal_Kembali);
            TimeSpan _TimeSpan = new TimeSpan();
            //_TimeSpan = TglPengembalian - TglJatuhTempo;
            _TimeSpan = TglPengembalian.Subtract(TglJatuhTempo);
            int TotalDays = _TimeSpan.Days;
            pengembalianBuku.Jumlah_Hari_Terlambat = Convert.ToString(TotalDays);
        }

Ajax

<script type="text/javascript" lang="javascript">
    $(document).ready(function () {
        $('#BtnHitung').on("click", function () {
        $.ajax({
            url: "@Url.Action("Hitung", "Pengembalian")",
            type: "POST",
            contentType: "application/json;charset=UTF-8",
            dataType: "json",
            success: function (result) {
                $('#Denda').val(result.Denda);
                $('#Jumlah_Hari_Terlambat').val(result.Jumlah_Hari_Terlambat);
                $("#Total_Denda").val(result.Total_Denda);
            }
        });
    });
});
</script>

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Aug 21, 2021 06:30 AM
on Aug 24, 2021 05:46 AM

Hi trisetia302,

Check this example. Now please take its reference and correct your code.

Model

public class Pengembalian
{
    [Key]
    [Display(Name = "Kode Kembali")]
    [Required(ErrorMessage = "Kode Kembali is required.")]
    public string Kode_Kembali { get; set; }

    [Display(Name = "Tanggal Pinjam")]
    [Required(ErrorMessage = "Tanggal Pinjam is required.")]
    public string Tanggal_Pinjam { get; set; }

    [Display(Name = "Tanggal Kembali")]
    [Required(ErrorMessage = "Tanggal Kembali is required.")]
    public string Tanggal_Kembali { get; set; }

    [Display(Name = "Tanggal Jatuh Tempo")]
    [Required(ErrorMessage = "Tanggal Jatuh Tempo is required.")]
    public string Tanggal_Jatuh_Tempo { get; set; }

    [Display(Name = "Denda")]
    [Required(ErrorMessage = "Denda is required.")]
    public string Denda { get; set; }

    [Display(Name = "Jumlah Hari Terlambat")]
    [Required(ErrorMessage = "Jumlah Hari Terlambat is required.")]
    public string Jumlah_Hari_Terlambat { get; set; }

    [Display(Name = "Total Denda")]
    [Required(ErrorMessage = "Total Denda is required.")]
    public string Total_Denda { get; set; }

    [Display(Name = "Kode Petugas")]
    [Required(ErrorMessage = "Kode Petugas is required.")]
    public string Kode_Petugas { get; set; }

    [Display(Name = "Nama Petugas")]
    [Required(ErrorMessage = "Nama Petugas is required.")]
    public string Nama_Petugas { get; set; }

    [Display(Name = "Kode Anggota")]
    [Required(ErrorMessage = "Kode Anggota is required.")]
    public string Kode_Anggota { get; set; }

    [Display(Name = "Nama Anggota")]
    [Required(ErrorMessage = "Kode Anggota is required.")]
    public string Nama_Anggota { get; set; }

    [Display(Name = "Kode Pinjam")]
    [Required(ErrorMessage = "Kode Pinjam is required.")]
    public string Kode_Pinjam { get; set; }
}

Controller

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View(new Pengembalian());
    }

    [HttpPost]
    public JsonResult Hitung([FromBody] Pengembalian pengembalianBuku)
    {
        CariSelisihHariTerlambatBalikinBuku(pengembalianBuku);
        CariDenda(pengembalianBuku);
        return Json(pengembalianBuku);
    }

    private void CariDenda(Pengembalian pengembalianBuku)
    {
        DateTime Tanggal_Kembali = Convert.ToDateTime(pengembalianBuku.Tanggal_Kembali);
        DateTime TglJatuhTempo = Convert.ToDateTime(pengembalianBuku.Tanggal_Jatuh_Tempo);

        if (Tanggal_Kembali <= TglJatuhTempo)
        {
            int Denda = 0;
            pengembalianBuku.Denda = Convert.ToString(Denda);
        }
        else if (Tanggal_Kembali > TglJatuhTempo)
        {
            decimal Denda = 2000;
            decimal JmlHariTerlambat = Convert.ToDecimal(pengembalianBuku.Jumlah_Hari_Terlambat);
            Denda = Convert.ToDecimal(pengembalianBuku.Denda);
            decimal Hasil = JmlHariTerlambat * Denda;
            pengembalianBuku.Total_Denda = Convert.ToString(string.Format("{0:0,00.00}", Hasil));
        }
    }

    private void CariSelisihHariTerlambatBalikinBuku(Pengembalian pengembalianBuku)
    {
        DateTime TglJatuhTempo = Convert.ToDateTime(pengembalianBuku.Tanggal_Jatuh_Tempo);
        DateTime TglPengembalian = Convert.ToDateTime(pengembalianBuku.Tanggal_Kembali);
        TimeSpan _TimeSpan = new TimeSpan();
        _TimeSpan = TglPengembalian.Subtract(TglJatuhTempo);
        int TotalDays = _TimeSpan.Days;
        pengembalianBuku.Jumlah_Hari_Terlambat = Convert.ToString(TotalDays);
    }
}

View

@model _153670_Calculation_DateTime_Core_MVC.Models.Pengembalian
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript" lang="javascript">
        $(document).ready(function () {
            $('#BtnHitung').on("click", function () {
                var obj = {};
                obj.Tanggal_Kembali = "2021/08/24";
                obj.Tanggal_Jatuh_Tempo = "2021/08/22";
                obj.Jumlah_Hari_Terlambat = "100";
                obj.Denda = "5";
                $.ajax({
                    url: "@Url.Action("Hitung", "Home")",
                    type: "POST",
                    data: JSON.stringify(obj),
                    contentType: "application/json;charset=UTF-8",
                    dataType: "json",
                    success: function (result) {
                        $('#Denda').val(result.Denda);
                        $('#Jumlah_Hari_Terlambat').val(result.Jumlah_Hari_Terlambat);
                        $("#Total_Denda").val(result.Total_Denda);
                    },
                    error: function (response) {
                    }
                });
            });
        });
    </script>
</head>
<body>
    <input type="button" id="BtnHitung" value="Hitung" />
    <hr />
    <input asp-for="Denda" id="Denda" class="form-control" readonly="readonly" />
    <input asp-for="Jumlah_Hari_Terlambat" id="Jumlah_Hari_Terlambat" class="form-control" readonly="readonly" />
    <input asp-for="Total_Denda" id="Total_Denda" class="form-control" readonly="readonly" />
</body>
</html>

Screenshot