現在の年-月-日 (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 < 10) month = `0${month}` // 如果月份是个位数,在前面补0
if (strDate < 10) strDate = `0${strDate}` // 如果日是个位数,在前面补0
return `${year}-${month}-${strDate}`
}
使い方
console.log(getNowFormatDate(), 'time') // 2022-06-22 time
現在の年、月、日(YYYY mm month dd day)形式を取得します。
//获取当前日期函数
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') // 2022 年 06 月 22 日 time
現在の年、月、日、週、時、分、秒を取得します
//获取当前日期函数
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') // 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(),
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') // 2022 年 06 月 22 日 星期三 15 : 55 : 46 time