1. <el-date-picker style="width: 100%" 2. v-model="form.statDate" 3. type="date" 4. :editable="false" 5. :clearable="false" 6. format="yyyy-MM-dd" 7. :picker-options="pickerOptions" 8. value-format="yyyy-MM-dd" 9. placeholder="请选择数据日期"> 10. </el-date-picker>
1. data() { 2. return { 3. pickerOptions: { 4. // 不能选择以后的日期 yyyy-MM-dd 5. disabledDate(time) { 6. // return time.getTime() > Date.now() - n * 24 * 60 * 60 * 1000; n等于减去多少天 7. let currentTime = new Date(); 8. currentTime.setTime(currentTime.getTime()); 9. let month = currentTime.getMonth() + 1; 10. let date = currentTime.getDate(); 11. let fullMonth = month < 10 ? '0' + month : month; 12. let fullDay = date < 10 ? '0' + date : date; 13. let baseTime = new Date( 14. currentTime.getFullYear() + '-' + fullMonth + '-' + fullDay, 15. ); 16. return time.getTime() > baseTime.getTime(); 17. } 18. } 19. } 20. }
1. //默认选中当前日期的前n天日期 yyyy-MM-dd 形式 2. getDefaultDay() { 3. // 或者 new Date(new Date().getTime() - n * 24 * 60 * 60 * 1000), 4. const date = new Date(); 5. date.setTime(date.getTime()-3600 * 1000 * 24 * n); 6. let dateDate = date; 7. let month = dateDate.getMonth() + 1; 8. let fullMonth = month < 10 ? '0' + month : month; 9. let day = 10. dateDate.getDate() < 10 ? '0' + dateDate.getDate() : dateDate.getDate(); 11. // 赋值即可 12. this.form.statDate = 13. dateDate.getFullYear() + '-' + fullMonth + '-' + day; 14. }