学无先后,达者为师

网站首页 前端文档 正文

js(javaScript)获取当前年月日,格式(YYYY-mm-dd、YYYY年mm月dd日)

作者:我总是词不达意 更新时间: 2022-09-22 前端文档

获取当前年月日(YYYY-mm-dd)格式

//获取当前日期函数
function getNowFormatDate() {
  let date = new Date(),
    seperator1 = '-', //格式分隔符
    year = date.getFullYear(), //获取完整的年份(4位)
    month = date.getMonth() + 1, //获取当前月份(0-11,0代表1月)
    strDate = date.getDate() // 获取当前日(1-31)
  if (month >= 1 && month <= 9) month = '0' + month // 如果月份是个位数,在前面补0
  if (strDate >= 0 && strDate <= 9) strDate = '0' + strDate // 如果日是个位数,在前面补0

  let currentdate = year + seperator1 + month + seperator1 + strDate 
  return currentdate
}

使用方式

let time = this.getNowFormatDate()
console.log(time, 'time') //  2022-06-22 time

获取当前年月日(YYYY年mm月dd日)格式

//获取当前日期函数
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 >= 1 && month <= 9) month = '0' + month // 如果月份是个位数,在前面补0
  if (strDate >= 0 && strDate <= 9) strDate = '0' + strDate // 如果日是个位数,在前面补0

  let currentdate = `${year} 年 ${month} 月 ${strDate} 日`
  return currentdate
}

使用方式

let time = this.getNowFormatDate()
console.log(time, 'time') //  2022 年 06 月 22 日 time

获取当前年月日星期时分秒

//获取当前日期函数
function getNowFormatDate() {
  let date = new Date(),
    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)

  if (month >= 1 && month <= 9) month = '0' + month // 如果月份是个位数,在前面补0
  if (strDate >= 0 && strDate <= 9) strDate = '0' + strDate // 如果日是个位数,在前面补0
  if (minute >= 0 && minute <= 9) minute = '0' + minute // 如果分是个位数,在前面补0
  if (second >= 0 && second <= 9) second = '0' + second // 如果秒是个位数,在前面补0

  let currentdate = `${year} 年 ${month} 月 ${strDate} 日 ${week} ${hour} : ${minute} :             
                     ${second}`
  return currentdate
}

使用方式

let time = this.getNowFormatDate()
console.log(time, 'time') //  2022 年 06 月 22 日 星期三 15 : 55 : 46 time

由于多个是个位数时需要在前补0,写一个公共方法

//小于10的拼接上0字符串 

function addZero(i) {
   return i < 10 ? ('0' + i) : i
}

使用方式 

//获取当前日期函数
function getNowFormatDate() {
  let date = new Date(),
    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)

  let currentdate = `${year} 年 ${addZero(month)} 月 ${addZero(strDate)} 日 ${week} ${hour}                         
                    : ${addZero(minute)} : ${addZero(second)}`
  return currentdate
}
let time = this.getNowFormatDate()
console.log(time, 'time') //  2022 年 06 月 22 日 星期三 15 : 55 : 46 time

到此结束。

原文链接:https://blog.csdn.net/weixin_43743175/article/details/125410163

栏目分类
最近更新