js获取 年-月-日 格式日期和 00:00:00格式时间

 一、将时间转化为 年-月-日 格式 函数

/**
 * @description: 将时间转化为 年-月-日 格式
 * @param timestamp
 * @return {String}
 */
getDateFormatted(timestamp) {
  let date
  if (timestamp) {
    date = new Date(timestamp) // 将时间转换为 Date 对象
  } else {
    date = new Date() // 获取当前时间
  }
  const year = date.getFullYear() // 获取年份
  const month = String(date.getMonth() + 1).padStart(2, '0') // 获取月份,并且补齐到两位数
  const day = String(date.getDate()).padStart(2, '0') // 获取日期,并且补齐到两位数
  const formattedDate = `${year}-${month}-${day}` // 拼接成 年-月-日 格式的字符串
  return formattedDate
}
const _date = new Date().getTime() + 3600 * 1000 * 24 * 7 // 获取一周后的日期
this.getDateFormatted(_date ) // 得到一周后的 年-月-日 格式 日期

二、获取当前时间 "00:00:00"格式 函数

/**
 * @description: 获取当前时间 "00:00:00"格式
 * @return {String}
 */
getCurrentTime() {
  const now = new Date() // 获取当前时间
  const hours = now.getHours().toString().padStart(2, '0') // 获取小时,并且补齐到两位数
  const minutes = now.getMinutes().toString().padStart(2, '0') // 获取分钟,并且补齐到两位数
  const seconds = now.getSeconds().toString().padStart(2, '0') // 获取秒数,并且补齐到两位数
  const currentTimeString = `${hours}:${minutes}:${seconds}` // 拼接成 时:分;秒 格式的字符串
  return currentTimeString
},
this.getCurrentTime() // 取当前时间 "00:00:00"格式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值