undefinedfix
Sign in

Ask JS to get 300 seconds (5 minutes) after the current time.

idontknow edited in Mon, 31 Oct 2022
var date = new Date(); // 假如当前时间 12:00:00
var hour = date.getHours() < 10 ? ('0' + date.getHours()) : date.getHours();
var minute = date.getMinutes() < 10 ? ('0' + date.getMinutes()) : date.getMinutes();
var second = date.getSeconds() < 10 ? ('0' + date.getSeconds()) : date.getSeconds();
var formatdate = hour + ":" + minute + ":" + second; // 获取到的时间 12:00:00

I now write:

var time = (function() {
    var period = [];
    var length = 300;
    while (length--) {
        period.push(formatdate);
        second = second + 1;
    }
    return period;
})();

The results are as follows:

["12:00:00" , "12:00:01" ,"12:00:02" ....... "12:00:59", "12:00:60", "12:00:61", "12:00:62".......]

The effect (array) you want to get is as follows:

["12:00:00" , "12:00:01" ,"12:00:02" ....... "12:04:59", "12:05:00"]

How to get it?

8 Replies
Scott
commented on Mon, 31 Oct 2022

Using the moment plug-in, the effect is as follows:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Example</title>
  </head>

  <body>
    <script src="https://cdn.bootcss.com/moment.js/2.24.0/moment.min.js"></script>
    <script>
      let currentTime = moment().format("YYYY-MM-DD HH:mm:ss");
      let result = [];
      for(let i = 0; i < 300; i++) {
        result.push(moment(currentTime).add(i, "seconds").format("HH:mm:ss"));
      }
      console.log(result);
    </script>
  </body>
</html>

Partial print results:

Youssef
commented on Mon, 31 Oct 2022

//Simple implementation

let now = new Date().getTime();
let ary = [];
for(let i=1;i<=300;i++){
    // formatdate 是一个时间格式方法
    ary.push(formatdate(now+i*1000))
}

//If it's complicated, it's more troublesome. According to the second + 1, the first digit after overflow + 1, we have to consider how many days there are in a certain month of a certain year

iNERyonGHTFu
commented on Tue, 01 Nov 2022
function addSec(date,times) {
    return new Date(date.getTime() + times*1000);
}

function fomatDate(date){
  let hour = date.getHours() < 10 ? ('0' + date.getHours()) : date.getHours();
  let minute = date.getMinutes() < 10 ? ('0' + date.getMinutes()) : date.getMinutes();
  let second = date.getSeconds() < 10 ? ('0' + date.getSeconds()) : date.getSeconds();
  return hour + ":" + minute + ":" + second; // 获取到的时间 12:00:00
}

function getSecArr(num){
  let result = []
  for(let i=1;i<=num;i++){
    result.push(fomatDate(addSec(new Date(),i)))
  }
  return result
}
getSecArr(300)
qgazn
commented on Tue, 01 Nov 2022
let now=new Date().getTime();
let out=[];
for(let i=0;i<300;i++){
   out.push( ((new Date( now + i*1000)).toTimeString() ).substring(0,8))
}

This should be the simplest.

user2035888
commented on Tue, 01 Nov 2022

What kind of time is second + 1? It can't be used like this. SetTime takes minutes and seconds after adding one second

user1300830
commented on Tue, 01 Nov 2022
const getTimeIn5Min = () => new Date((new Date()).valueOf() + 300000);

Look at the process of writing format, the next thing should be OK.

user3241323
commented on Tue, 01 Nov 2022

It should be OK to deal with it in your way

var date = new Date();
var hour = date.getHours()
var minute = date.getMinutes()
var second = date.getSeconds()
var formatdate
var time = (function() {
    var period = [];
    var length = 300;
    while (length--) {
        if(second>=60){
            second=0
            minute+=1
        }
        if(minute>=60){
            minute=0
            hour+=1
        }
        if(hour>=24){
            hour=0
            minute=0
            second=0
        }
        var formatdate = ("0"+hour).slice(-2) + ":" + ("0"+minute).slice(-2) + ":" + ("0"+second).slice(-2); 
        period.push(formatdate);
        second = second + 1;
    }
    return period;
})();
mmeisner
commented on Tue, 01 Nov 2022
Date.prototype.format = function(formatStr = 'yyyy-MM-dd'){// 格式化显示Date对象方法format
  let str = formatStr
  let Week = ['日', '一', '二', '三', '四', '五', '六']
  str = str.replace(/yyyy|YYYY/, this.getFullYear())
  str = str.replace(
    /yy|YY/,
    (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100)
  )
  let month = this.getMonth() + 1
  str = str.replace(/MM/, month > 9 ? month.toString() : '0' + month)
  str = str.replace(/M/g, month)
  str = str.replace(/w|W/g, Week[this.getDay()])
  str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate())
  str = str.replace(/d|D/g, this.getDate())
  str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours())
  str = str.replace(/h|H/g, this.getHours())
  str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes())
  str = str.replace(/m/g, this.getMinutes())
  str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds())
  str = str.replace(/s|S/g, this.getSeconds())
  return str
}

let gap = 1000,// 1000毫秒
len = 5 * 60 * 1000 / gap; // 5分钟 有多少个gap
console.log((
  /**
  * @param {number} gap 时间间距单位为 ms
  * @param {number} len 想要的长度
  */
  (gap, len)=>{
    let tNow = new Date().getTime(),
    re = [];
    for(let k=0; k++<len;){
      re.push(new Date(tNow + k*gap).format('hh:mm:ss'))
    }
    return re
  }
)(gap, len))