Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <script type="x/template" id="date-picker-template">
  <div class="date-picker form-inline">
    <div class="form-group">
      <select class="form-control year" v-model="year" @change="onChangeYear">
        <option value="">......</option>
        <option v-for="year in years" :value="year">{{year}}</option>
      </select>
      <label>Year</label>
    </div>
    <div class="form-group">
      <select class="form-control mouth" v-model="month" @change="onChangeMonth">
        <option value="">......</option>
        <option v-for="month in months" :value="month">{{month+1}}</option>
      </select>
      <label>Mouth</label>
    </div>
    <div class="form-group">
      <select class="form-control day" v-model="day" @change="onChangeDay">
        <option value="">......</option>
        <option v-for="day in days" :value="day">{{day}}</option>
      </select>
      <label>Day</label>
    </div>
  </div>
</script>

<div id="date-picker-demo">
  <date-picker :format="format" start="1950.03.03" :default="date" end="2016.02.05" @date-change="showDate"></date-picker>
  Current date: {{date}}
</div>
              
            
!

CSS

              
                body
  padding 20px
.date-picker
  .year
  .mouth
  .day  
    width 120px
.form-group
  margin-right 10px
              
            
!

JS

              
                Vue.component('date-picker', {
  template: '#date-picker-template',
  props: {
    format: {
      type: String,
      default: 'YYYYMMDD'
    },
    start: {
      type: String,
      default: '19000101'
    },
    default: {
      type: String,
      default: '19900101'
    },
    end: {
      type: String,
      default: '20201231'
    }
  },
  data: function() {
    return {
      year:  '',
      month: '',
      day: '',
      years: [],
      months: [],
      days: [],
      startDate: null,
      endDate: null
    };
  },
  computed: {
    date: {
      get: function() {
        return moment({
          year: this.year,
          month: this.month,
          day: this.day
        });
      },
      set: function(v) {
        var _d = null;
        if (moment.isMoment(v)) {
          _d = v;
        } else {
          _d = moment(v, this.format);
        }
        if(!_d.isValid()) {
          throw new Error('Not valid moment or not valid time for format: ', this.format);
        }
        this.year = _d.year();
        this.month = _d.month();
        this.day = _d.date();
        console.log('Current date: ', this.year, this.month, this.day);
      }
    }
  },
  created: function() {
    var _s = moment(this.start, this.format);
    if (!_s.isValid()) {
      throw new Error('Start date is not right, or format is not right.');
    }
    var _d = moment(this.default, this.format);
    if (!_d.isValid()) {
      throw new Error('Default date is not right, or format is not right.');
    }
    var _e = moment(this.end, this.format);
    if (!_e.isValid()) {
      throw new Error('End date is not right, or format is not right.');
    }
    if (!_s.isBefore(_e)) {
      throw new Error('Start date must before end date.');
    }
    if (!_d.isBetween(_s, _e)) {
      throw new Error('Default date must between start and end.');
    }
    this.startDate = {year: _s.year(), month: _s.month(), day: _s.date()};
    this.endDate = {year: _e.year(), month: _e.month(), day: _e.date()};
    this.date = _d;
    this.getYears();
    this.getMonths();
    this.getDays();    
  },
  methods: {
    getYears: function() {
      var _to = this.endDate.year;
      for (var _y = this.startDate.year; _y <= _to; _y++) {
        this.years.push(_y);
      }
    },
    getMonths: function() {
      this.months = [];
      if (this.year === '') return;
      var _to = 11;
      var _m = 0;
      if (this.year >= this.endDate.year) {
        _to = this.endDate.month;
      }
      if (this.year <= this.startDate.year) {
        _m = this.startDate.month;
      }
      for (; _m <= _to; _m++ ) {
        this.months.push(_m);
      }
    },
    getDays: function() {
      this.days = [];
      if (this.year === '' || this.month === '') return;
      var _to = this.date.daysInMonth();
      var _d = 1;
      if (this.year <= this.startDate.year && this.month <= this.startDate.month) {
        _d = this.startDate.day;
      }
      if (this.year >= this.endDate.year && this.month >= this.endDate.month) {
        _to = this.endDate.day;
      }
      for (; _d <= _to; _d++) {
        this.days.push(_d);
      }
    },
    onChangeYear: function() {
      this.month = '';
      this.day = '';
      this.getMonths();
      this.getDays();
      this.$dispatch('date-change', this.date);
    },
    onChangeMonth: function() {
      this.day = '';
      this.getDays();
      this.$dispatch('date-change', this.date);
    },
    onChangeDay: function() {
      this.$dispatch('date-change', this.date);
    }
  }
});

new Vue({
  el: '#date-picker-demo',
  data: {
    format: 'YYYY.MM.DD',
    date: '1991.10.01'
  },
  methods: {
    showDate: function(date) {
      this.date = date.format(this.format);
    }
  }
});
              
            
!
999px

Console