(js) encapsulates the year, month, and day acquisition method. The page displays the current year, year, month, and date based on type judgment.

(js) encapsulates the year, month, and day acquisition method. The page displays the current year, year, month, and date based on type judgment.


Project 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
  }
  
}

Page usage

<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>

Guess you like

Origin blog.csdn.net/qq_44754635/article/details/133709772