简体   繁体   中英

How to change format of date in function

I have this Javascript snippet that retrieves a start date from combobox and compares it to an end date, it then works out how many months are in between the start and end date and outputs that value into another textfield, then in the end takes the end date and adds one day to it and sets that value as the expiry date, its a simple function however I cant figure out how to fix the format of the dates Right now when a person enters the start and end date the format is in Y,m,d this needs to be changed to Ymd (with hyphens), however this affects the function itself and then it does not work, I can easily change the format in extjs but I need to make sure the function still works after

Here is the function

function monthDiff(startdate, enddate) {
    var months;
    months = (enddate.getFullYear() - startdate.getFullYear()) * 12;
    months -= startdate.getMonth();
    months += enddate.getMonth();
    return months <= 0 ? 0 : months;
}
var m = monthDiff(
    Ext.getCmp('startdateTextField').getValue(),
    Ext.getCmp('enddateTextField').getValue()
);
Ext.getCmp('durationTextField').setValue(m);

Date.prototype.addDays = function(d) {
    this.setDate(this.getDate() + d);
    return this;
};
Date.prototype.yyyymmdd = function() {
    var yyyy = this.getFullYear().toString();
    var mm = (this.getMonth() + 1).toString();
    var dd = this.getDate().toString();
    return yyyy + '-' + (mm[1] ? mm : "0" + mm[0]) + '-' + (dd[1] ? dd : "0" + dd[0]);
};

var end = Ext.getCmp('enddateTextField').getValue();
end.addDays(1);
var enddate = end.yyyymmdd();
var expirydate = enddate;
Ext.getCmp('expirydateTextField').setValue(expirydate);

Let me know if I should post the extjs form code

如果您使用的是extJS,我强烈建议您使用Ext.Datehttp://docs.sencha.com/extjs/4.2.1/#!/api/Ext.Date )类来处理,格式化和解析日期。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM