Calculate the number of days between two dates within the same ASPxDateEdit Control using JavaScript

I am working with an ASPxDateEdit control and here is the code snippet:

<dx:ASPxDateEdit ID="dtPayDate" ClientInstanceName="dateEdit" EditFormatString="yyyy-MM-dd" DisplayFormatString="yyyy-MM-dd" runat="server" EnableClientSideAPI="true" CssClass="form-control">
<ClientSideEvents Init="FirstDate()" LostFocus="ChangeDate()" />

This code includes two events:

var InitialPay;
var FinalPay;

function FirstDate(s, e) {
    var date = s.GetDate();
    var dd = date.getDate();
    var mm = date.getMonth();
    var yy = date.getFullYear();
    InitialPay = new Date(yy + ',' + mm + ',' + dd);
}

function ChangeDate() {
    var jsDate = dateEdit.GetDate();
    var year = jsDate.getFullYear(); 
    var month = jsDate.getMonth(); 
    var day = jsDate.getDate(); 

    FinalPay = new Date(year + ',' + month + ',' + day);

    var a = moment([FinalPay]);
    var b = moment([InitialPay]);
    days = a.diff(b, 'days') 
    alert(days)
}

The requirement is to capture the initial date before any changes are made and store it in one variable. Then, capture the changed date and save it in another variable. Finally, calculate the difference between these two dates. The current implementation utilizes moment.js, but other methods can be considered.

Answer №1

To effectively handle date calculations, it is recommended to utilize the GotFocus event instead of the Init event:

<dx:ASPxDateEdit ID="dtPayDate" ClientInstanceName="dateEdit" EditFormatString="yyyy-MM-dd" DisplayFormatString="yyyy-MM-dd" runat="server" EnableClientSideAPI="true" CssClass="form-control">
    <ClientSideEvents GotFocus="FirstDate" LostFocus="ChangeDate"/>
</dx:ASPxDateEdit>

You can easily calculate the difference between two dates using basic arithmetic operations. Here's a sample code snippet for reference:

var InitialDate;
var FinalDate;

function FirstDate(s, e) {
    InitialDate = s.GetDate();
}

function ChangeDate(s, e) {
    FinalDate = s.GetDate();

    if (!!InitialDate && !!FinalDate)
    {
        days = (FinalDate - InitialDate) / (1000 * 60 * 60 * 24);
        alert(days);
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Customizing the CSS for pagination in an Asp GridView displays correctly in the Design View, but does not

Using Bootstrap, I have implemented custom CSS for pagination in a gridview. My master page includes: <link href="css/bootstrap.min.css" rel="stylesheet"> <link href="css/style.css" rel="stylesheet"> In the aspx page, my gridview code looks l ...

Tips for customizing MUI PaperProps using styled components

I am trying to customize the width of the menu using styled components in MUI. Initially, I attempted the following: const StyledMenu = styled(Menu)` && { width: 100%; } `; However, this did not have any effect. After further research, I ...

Retrieve a specific div within another div by targeting both parent and child classes dynamically

I am working with the following structure: <div class="fc-event lifeguard"> <div class="fc-event-inner"> </div> </div> Inside a javascript function, I am trying to achieve this: $('.lifeguard').find('.fc- ...

Cannot chain promises using 'then'

Having trouble understanding why the 'describeDir' promise chain is not working properly. Can anyone help me figure out what I did wrong here? Everything in the code seems to run, but functions like then or finally from the promise API never get ...

Angular 5 provides a feature where a button can be used to redirect to a

Hey there! I am currently diving into the world of Angular 5 for the first time and I've run into a bit of a roadblock. I'm trying to figure out how to redirect from one page to another using a button click. Here's the code snippet I have: ...

React component making repeated calls to my backend server

As a React newbie, I am currently in the process of learning and exploring the framework. I recently attempted to fetch a new Post using axios. However, upon hitting the '/getPost' URL, I noticed that the GetPost component continuously calls the ...

Error Alert: jQuery Ajax Not Executing

Looking to send form data to PHP using jQuery. Check out the code below. -------------HTML FORM-------------- <div id="createQuestionBlock"> <form id="createQuestionForm" action="" method="POST"> Question Code: <input id="code" ...

Unveiling the Power of Source-Maps: A Guide to Debugging Local NPM Dependencies Using `npm link`

Currently, I am utilizing babel-cli to compile the source code of my local NPM dependency. This is how my package.json file looks like: "main": "lib/index.js", "scripts": { "dev": "babel src --watch -d lib --source-maps inline", }, My other applicat ...

Creating a dynamically expanding rowspan table using VueJS and an associative array with 5 levels of depth

This challenge appears to be quite daunting, but let's dive in Essentially, I have an associative array that can be altered based on various filters (columns may appear or disappear depending on the selected filters). Additionally, there is a table w ...

Can Promise be used as a tool for implementing asynchronous programming?

I've been delving into the Promise functionality on Google's source code, but have yet to uncover how it handles executing code asynchronously. My current understanding of asynchronous functions is that code following them may be resolved before ...

What causes the disparity in height between a textbox and the encompassing span element?

What causes the discrepancy in element heights when looking at the code below? <span id="spanner" style="margin:0;padding:0;border:0;outline:0;"><input type="text" /></span> If you check the height of the text box, it will be shown as 1 ...

Material-UI organizes its Grid Items vertically, creating a column layout rather than a horizontal row

I'm struggling to create a grid with 3 items in each row, but my current grid layout only displays one item per row. Each grid item also contains an image. How can I modify the code to achieve a grid with 3 items in a row? Here's the code snippet ...

Cufon embedding itself into every element

It seems like Cufon is automatically injecting itself into the JavaScript AJAX response, causing a 404 error that impacts the success of the operation. You can experience this if you visit: and hover your mouse over a product image. The AJAX functionalit ...

The TypeScript datatype 'string | null' cannot be assigned to the datatype 'string'

Within this excerpt, I've encountered the following error: Type 'string | null' cannot be assigned to type 'string'. Type 'null' cannot be assigned to type 'string'. TS2322 async function FetchSpecificCoinBy ...

Interactive sidebar component with navigation and animated section markers

For weeks, I've been attempting to create a navigation sidebar similar to the ones shown in these images: Even though getbootstrap.com/components offers appealing navigation sidebars, I have not found a built-in component in their library. This has m ...

My code gets disrupted when I switch between ids and classes

I'm currently learning about javascript and jquery, attempting to implement various scripts. I successfully executed the following script: jQuery(document).ready(function($) { var scroll_start = 0; var startchange = $('#homepage-header' ...

What is the best way to add style to the title attribute of a dropdown menu item?

In my webform project, I have implemented a dropdown menu with custom tooltips for the list items. These tooltips are different from the display field and value field, which can be considered as the third column. To bind the tooltips to the list items usin ...

Manipulate values within an array when a checkbox is selected or deselected

I am developing an Angular application where I have created a checkbox that captures the change event and adds the checked value to an array. The issue I am facing is that even if the checkbox is unchecked, the object is still being added to the array. D ...

React Native - Implementing a dynamic form that adapts based on the answer given by its parent

My JavaScript Object has a simple state structure as follows: pertanyaan: [{ label: "label1", type: 'dropdown', key: 'keyFoo1', option: [{ value: "foo1" }, { value: "foo2", additional ...

What is the best way to initiate actions once the form has been successfully processed?

Could someone assist me with creating a form that hides after submission and displays a thank you message instead? I've tried the code below, but it seems that the action isn't being executed once the form is submitted. It's as if the ' ...