js gets the current year, month, and day in the format (YYYY mm month dd day)

1. uni-app displays the current system date (for example: 2023-01-21)

<template>
    <view class="content">
        {
   
   {date}}
    </view>
</template>
<script>
    export default {
        data() {
            return {
                date: new Date().toISOString().slice(0, 10), // 2023-01-21
            }
        },
    }
</script>

2. Get the current year, month, day (YYYY mm month dd day) format

//获取当前日期函数
function getNowFormatDate() {
  let date = new Date(),
    year = date.getFullYear(),              //获取完整的年份(4位)
    month = date.getMonth() + 1,            //获取当前月份(0-11,0代表1月)
    strDate = date.getDate()                // 获取当前日(1-31)
  if (month < 10) month = `0${month}`       // 如果月份是个位数,在前面补0
  if (strDate < 10) strDate = `0${strDate}` // 如果日是个位数,在前面补0
  return `${year}年${month}月${strDate}日`
  console.log(getNowFormatDate(), 'time')   //  2023-01-21 time
}

3. Get the current year, month, day, week, hour, minute, and second

//获取当前日期函数
function getNowFormatDate() {
  let date = new Date(),
    obj = {
      year: date.getFullYear(),            //获取完整的年份(4位)
      month: date.getMonth() + 1,          //获取当前月份(0-11,0代表1月)
      strDate: date.getDate(),             // 获取当前日(1-31)
      week: '星期' + '日一二三四五六'.charAt(date.getDay()), //获取当前星期几(0 ~ 6,0代表星期天)
      hour: date.getHours(),               //获取当前小时(0 ~ 23)
      minute: date.getMinutes(),           //获取当前分钟(0 ~ 59)
      second: date.getSeconds()            //获取当前秒数(0 ~ 59)
    }
 
  Object.keys(obj).forEach(key => {
    if (obj[key] < 10) obj[key] = `0${obj[key]}`
    // console.log(obj[key])
  })
 
  return `${obj.year}年${obj.month}月${obj.strDate}日${obj.week} ${obj.hour}:${obj.minute}:${obj.second}`
    
  console.log(getNowFormatDate(), 'time') //  2023 年 01 月 21 日 星期六 15 : 55 : 46 time
}

Guess you like

Origin blog.csdn.net/m0_69257679/article/details/128644119