Extjs calculates the month (days) between two datefields)

Source: Internet
Author: User

Requirement: Two DateField controls: Start Time and end time. After the end time is selected, the month or number of days between the two time periods are automatically calculated.

Problems to be Solved:

1. The date obtained by directly using Ext. getCmp ('enddate'). getValue () cannot be used directly. You need to Format it using Ext. util. Format. date ().

2. The time difference calculated by default is millisecond, which requires conversion. 1 day = 86400000 milliseconds.

3. Use the Math. round () function to obtain the number.

4. Add a monitoring event to the date control of the end time.

[Javascript]
Var serviceTimeStart = new Ext. form. DateField ({
Name: "serviceTimeEnd", // The latest payment time parameter is the end time of the previous period
Id: "startdate ",
FieldLabel: "service start time ",
AllowBlank: false,
Width: 180,
TabIndex: 8,
Format: 'Y-m-d ',
BlankText: "cannot be blank"

});
Var serviceTimeEnd = new Ext. form. DateField ({
Name: "serviceTimeEnd ",
Id: "endDate ",
FieldLabel: "service termination time ",
Width: 180,
TabIndex: 9,
Format: 'Y-m-d ',

Listeners :{
Change: function ()
{
 
Var e = Ext. util. Format. date (Ext. getCmp ('enddate'). getValue (), 'Y-m-d'); // Format the date Control Value
Var s = Ext. util. Format. date (Ext. getCmp ('startdate'). getValue (), 'Y-m-d'); // Format the date Control Value
 
Var end = new Date (e );
Var start = new Date (s );
Var elapsed = Math. round (end. getTime ()-start. getTime ()/(86400000*30); // calculates the number of months at an interval.
 
Ext. MessageBox. alert (elapsed );
}
 
});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.