(js)封装年月日获取方法,页面根据type判断显示当前年,年月,日期

(js)封装年月日获取方法,页面根据type判断显示当前年,年月,日期


项目src——>utils——>index.js

// 获取当前年,年月,日期,type,
export function getYearMonth(type) {
  var date = new Date()
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  month = (month > 9) ? month : ('0' + month)
  day = (day < 10) ? ('0' + day) : day
  var today = year + '-' + month + '-' + day
  if(type == 'year') {
    return year
  }else if(type == 'month') {
    return year + '-' + month
  }else {
    return today
  }
  
}

页面使用

<el-form-item class="time-box" label>
  <el-date-picker
    v-model="query.date"
    type="month"
    value-format="yyyy-MM"
    placeholder="请选择"
    clearable
    class="time-select1"
    @change="selectChange($event, 'date')"
  />
</el-form-item>

<script>
import { getYearMonth } from "@/utils";

data() {
   return {
     query: { 
       date: getYearMonth("month"),
     },
   }
}

</script>

你可能感兴趣的:(JS,javascript,前端,开发语言)