简体   繁体   English

如何在功能中更改日期格式

[英]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 我有这个Javascript片段,可从组合框检索开始日期并将其与结束日期进行比较,然后计算出开始日期和结束日期之间有多少个月,然后将该值输出到另一个文本字段中,然后最后结束日期并为其添加一天并将该值设置为到期日期,这是一个简单的功能,但是我无法弄清楚如何确定日期的格式现在,当一个人输入开始日期和结束日期时,格式为Y, m,d这需要更改为Ymd(带连字符),但这会影响函数本身,然后它不起作用,我可以轻松地在extjs中更改格式,但是我需要确保函数在之后仍然可以正常工作

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表单代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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