(js)封装年月日获取方法,页面根据type判断显示当前年,年月,日期
项目src——>utils——>index.js
// 获取当前年,年月,日期,type,
export function getYearMonth(type) {var date = new Date()var year = date.getFullYear()var month = date.getMonth() + 1var day = date.getDate()month = (month > 9) ? month : ('0' + month)day = (day < 10) ? ('0' + day) : dayvar today = year + '-' + month + '-' + dayif(type == 'year') {return year}else if(type == 'month') {return year + '-' + month}else {return today}}
页面使用
<el-form-item class="time-box" label><el-date-pickerv-model="query.date"type="month"value-format="yyyy-MM"placeholder="请选择"clearableclass="time-select1"@change="selectChange($event, 'date')"/>
</el-form-item><script>
import { getYearMonth } from "@/utils";data() {return {query: { date: getYearMonth("month"),},}
}</script>