js gets the first xxx time and formatted time of the current date and time

Get the difference between the current time and the start time

1. The date 15 minutes before the current time

//获取xxx分钟前的时间日期
function timeFormat(minute) {
  //60000毫秒 代表1分钟
  let state = new Date(new Date().getTime() - minute * 60000),
      date = new Date(state),
      minutes = date.getMinutes()
  minutes < 0 ? minutes = `0${minutes}` : minutes = minutes
  //转换格式
  return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()} ${date.getHours()}:${minutes}`
}
console.log(timeFormat(15), '当前日期的前15分钟') //2022/10/28 23:55

2. The date one hour before the current time

//获取xxx小时前的时间日期
function timeFormat(hour) {
  let state = new Date(new Date().getTime() - hour * 60 * 60 * 1000),
    date = new Date(state),
	minutes = date.getMinutes()
  minutes < 0 ? minutes = `0${minutes}` : minutes = minutes
  //转换格式
  return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()} ${date.getHours()}:${minutes}`
}
console.log(timeFormat(1), '当前日期的前1个小时') 2022/10/28 23:15

It ends here.

Guess you like

Origin blog.csdn.net/weixin_43743175/article/details/127581419