Javascript 如果日期小于12,为什么我的日期和月份会被替换?
问题 如果我选择的日期小于12date,则它将以这种格式显示MM/D/YYYY,如果日期大于12,则输出为DD/M/YYYY。无论用户选择什么,我都希望获得这种格式。我还使用了prototype.js。任何帮助都将不胜感激 代码Javascript 如果日期小于12,为什么我的日期和月份会被替换?,javascript,prototypejs,Javascript,Prototypejs,问题 如果我选择的日期小于12date,则它将以这种格式显示MM/D/YYYY,如果日期大于12,则输出为DD/M/YYYY。无论用户选择什么,我都希望获得这种格式。我还使用了prototype.js。任何帮助都将不胜感激 代码 var Recurrence = Class.create({ // takes a JSON object with pattern options initialize: function (pattern, date_format) { if (t
var Recurrence = Class.create({
// takes a JSON object with pattern options
initialize: function (pattern, date_format) {
if (typeof pattern != 'object') throw new TypeError('pattern must be a JSON');
if (!pattern.every || pattern.every.blank()) {
throw new ReferenceError('Every magnitude must be specified');
}
if (isNaN(parseInt(pattern.every))) {
throw new TypeError('Every magnitude must be a valid number');
}
// stores generated dates based on recurrence pattern
this.dates = [];
this.start = Date.parse(pattern.start);
this.every = parseInt(pattern.every);
this.unit = pattern.unit;
this.end_condition = pattern.end_condition;
this.until = Date.parse(pattern.until);
this.rfor = parseInt(pattern.rfor);
this.occurrence_of = pattern.occurrence_of;
this.nth = parseInt(pattern.nth);
this.radioSelection = pattern.radioSelection;
this.indefinate = Date.parse(pattern.indefinate);
this.days = (pattern.days) ? pattern.days.sort() : [];
this.date_format = date_format || 'dd/M/yyyy';
this.month_date_format = date_format || 'dd';
},
// tries to describe the pattern in plain english
describe: function () {
var units = {'d': 'day', 'w': 'week', 'm': 'month', 'y': 'year'};
var week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'day'];
var nthword = ['', 'first', 'second', 'third', 'fourth', 'fifth', 'last']
var t = ['Every'];
if (this.every > 2) {
t.push(this.every, units[this.unit] + 's');
} else if (this.every == 2) {
t.push('other', units[this.unit]);
} else {
t.push(units[this.unit]);
}
//alert(this.radioSelection);
//alert(this.end_condition);
if (this.unit == 'w') {
var d = [];
for (var i = 0; i < this.days.length; i++) {
d.push(week[this.days[i]]);
}
t.push('on', d.join(', '));
} else if (this.unit == 'm') {
// check if it's a special word
day_idx = (this.occurrence_of < 0) ? week.length - 1 : this.occurrence_of;
nth_idx = (this.nth < 0) ? nthword.length-1 : this.nth;
//alert(this.radioSelection);
if(this.radioSelection == 'weekday'){
t.push('on the', nthword[nth_idx], week[day_idx]);
}else{
t.push('on date ', this.start.toString(this.month_date_format));
}//end if..
}
t.push('starting on', this.start.toString(this.date_format));
if (this.end_condition == 'until') {
t.push('until', this.until.toString(this.date_format));
} else if (this.end_condition == 'for') {
t.push('for', this.rfor, 'occurrences');
} else if(this.end_condition == 'indefinate'){
t.push('ends never.');//alert('sds');
}
return t.join(' ');
},
// determine whether given date is in recurrence
contains: function (d) {
if (this.dates.length == 0) this.generate();
// can be string or date object already
d = Date.parse(d);
for (var i = 0; i < this.dates.length; i++) {
if (Date.equals(this.dates[i], d)) return true;
}
return false;
},
// returns an array of dates base on input pattern
generate: function (max) {
if (!(this.rfor || this.until || max)) {
throw new RangeError('There is no valid end condition specified');
}
var end_condition_reached = function (occurrences, current_date) {
if (max && occurrences.length >= max) return true;
if (this.end_condition == 'for' && this.rfor && occurrences.length >= this.rfor) return true;
if (this.end_condition == 'until' && this.until && current_date > this.until) return true;
if (this.end_condition == 'indefinate' && this.indefinate && current_date > this.indefinate) return true;
return false;
}.bind(this);
var dates = [];
var curr = this.start.clone().clearTime();
// always include start date in recurrence
dates.push(curr.clone());
// weekly recurrence
if (this.unit == 'w') {
// if it's not already a sunday, move it to the current week's sunday
if (!curr.is().sunday()) curr.last().sunday();
if (this.days.length == 0) {
throw new RangeError('Weekly recurrence was selected without any days specified.');
return null;
}
while (!end_condition_reached(dates, curr)) {
// scan through the checked days
this.days.each(function (d) {
if (curr.getDay() < d) curr.moveToDayOfWeek(d);
if (curr <= this.start) return;
if (end_condition_reached(dates, curr)) return;
dates.push(curr.clone());
}.bind(this));
// rewind back to sunday
if (!curr.is().sunday()) curr.last().sunday();
// next repetition
curr.addWeeks(this.every);
}
} else if (this.unit == 'm') {
while (true) {
if (this.occurrence_of == -1) {
curr.moveToLastDayOfMonth();
} else {
if(this.radioSelection == 'weekday'){
curr.moveToNthOccurrence(this.occurrence_of, this.nth);
}else{
//this.occurrence_of = this.faltu_date;
}//end if
}//end if..
if (end_condition_reached(dates, curr)) break;
if (curr > this.start) {
dates.push(curr.clone());
}//end if..
curr.addMonths(this.every);
}//end while..
} else {
while (true) {
if (this.unit == 'd') {
curr.addDays(this.every);
} else if (this.unit == 'y') {
curr.addYears(this.every);
}
// else infinite loop yay
if (end_condition_reached(dates, curr)) break;
dates.push(curr.clone());
}
}
// cache results
this.dates = dates;
return this.dates;
}
});
var Recurrence=Class.create({
//使用模式选项获取JSON对象
初始化:函数(模式、日期\格式){
如果(typeof pattern!=“object”)抛出新的TypeError(“pattern必须是JSON”);
如果(!pattern.every | | pattern.every.blank()){
抛出新的ReferenceError('必须指定每个大小');
}
if(isNaN(parseInt(pattern.every))){
抛出新的TypeError('每个大小必须是有效的数字');
}
//根据重复模式存储生成的日期
此参数为.dates=[];
this.start=Date.parse(pattern.start);
this.every=parseInt(pattern.every);
this.unit=pattern.unit;
this.end_条件=pattern.end_条件;
this.until=Date.parse(pattern.until);
this.rfor=parseInt(pattern.rfor);
this.occurrence\u of=pattern.occurrence\u of;
this.nth=parseInt(pattern.nth);
this.radioSelection=pattern.radioSelection;
this.indefinite=Date.parse(pattern.indefinite);
this.days=(pattern.days)?pattern.days.sort():[];
this.date_format=date_format||'dd/M/yyyy';
this.month_date_format=date_format|||'dd';
},
//试图用通俗易懂的英语描述这种模式
描述:函数(){
变量单位={'d':'day','w':'week','m':'month','y':'year'};
var week=['星期日'、'星期一'、'星期二'、'星期三'、'星期四'、'星期五'、'星期六'、'日'];
var nthword=[''‘第一’、‘第二’、‘第三’、‘第四’、‘第五’、‘最后’]
var t=['每'];
如果(此值大于2){
t、 推(this.every,units[this.unit]+'s');
}else if(this.every==2){
t、 推送(“其他”,单位[本单位]);
}否则{
t、 推(单位[本单位]);
}
//警惕(本次选举);
//警报(此结束条件);
如果(this.unit='w'){
var d=[];
for(var i=0;i=max)返回true;
如果(this.end_condition=='for'&&this.rfor&&occurrencess.length>=this.rfor)返回true;
if(this.end_condition=='until'&&this.until&¤t_date>this.until)返回true;
如果(this.end_condition=='indefinite'&&this.indefinite&¤t_date>this.indefinite)返回true;
返回false;
}.约束(本);
var日期=[];
var curr=this.start.clone().clearTime();
//在重复周期中始终包括开始日期
dates.push(curr.clone());
//每周复发
如果(this.unit='w'){
//如果它还不是星期天,请将其移动到当前星期的星期天
如果(!curr.is().sunday())curr.last().sunday();
如果(this.days.length==0){
抛出新的RangeError('选择了每周重复,但未指定任何天数');
返回null;
}
当(!结束条件(日期、当前)){
//浏览已检查的日期
此。天。每个功能(d){
如果(curr.getDay()