1

问题

如果我选择小于12的日期,则以 MM/D/YYYY格式显示,如果日期大于12,则输出为DD/M/YYYY。无论用户选择什么,我都想获得这种格式DD/M/YYYY 。我也在使用protoype.js。任何帮助都感激不尽。

代码

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;
  }
});
4

0 回答 0