【问题标题】:How to get minutes in between two different dates?如何获得两个不同日期之间的分钟数?
【发布时间】:2020-08-20 15:19:22
【问题描述】:

您好,我正在使用 Javascript,我想获取两个日期之间的每一分钟,例如:

firstDate: 2019-04-02 02:03:00
secondDate: 2019-04-03 03:04:00

所以我的最终输出结果应该是这样的:

2019-04-02 02:03:00
2019-04-02 02:04:00
2019-04-02 02:05:00
.
.
.
2019-04-03 03:04:00

这是我尝试过的代码

var boxingDay = new Date("2019-04-02 02:03:00");
var nextWeek  = new Date("2019-04-03 03:04:00");

function getDatesRange(startDate, stopDate){
    const ONE_DAY = 60*1000;
    var days= [];
    var currentDate = new Date(startDate);
    while (currentDate <= stopDate) {
        days.push(new Date (currentDate));
        currentDate = currentDate - 1 + 1 + ONE_DAY;
    }
    return days.join("\n");
}

console.log(getDatesRange(boxingDay,nextWeek))

/* var map = getDates(boxingDay, nextWeek).map((times) => {
console.log(Date.parse(times))
}) */

/* console.log((getDates( boxingDay, nextWeek ))); */

问题是我得到了正确的输出,但我需要一个数组的形式,如下所示,如果我重复使用该函数,它会返回一个空数组。

[[2019-04-02 02:03:00],[2019-04-02 02:04:00].....]

任何解决方案 TIA。

【问题讨论】:

  • 忽略评论,很困惑问题的标题与问题无关
  • 只是为了让我清楚,您希望将上述答案放在一个数组中,对吗?
  • 我希望它在数组中,并且如果我使用它,我的代码中还需要函数重用它会返回一个空数组

标签: javascript node.js


【解决方案1】:

以你的代码为基础,你可以这样做(注意我用的是.toISOString(),你可以根据自己的需要改变这个):

const boxingDay = new Date("2019-04-02 02:03:00");
const nextWeek  = new Date("2019-04-03 03:04:00");

function getDatesRange(startDate, stopDate){
    const ONE_MINUTE = 60*1000;
    const days= [];
    let currentDate = new Date(startDate);
    while (currentDate <= stopDate) {
        days.push([currentDate.toISOString()]);
        currentDate.setTime(currentDate.getTime() + ONE_MINUTE);
    }
    return days;
}

console.log(getDatesRange(boxingDay,nextWeek));

【讨论】:

  • 感谢 eol 的回答,但是当我尝试在其他文件中重用时,它返回一个空数组。
  • 其他文件中的函数怎么调用?
  • 我的意思是“你能在调用函数的地方添加代码吗?” :)
【解决方案2】:

有一种方法,您可以使用带有格式功能的while 循环,每次迭代都创建分钟

const firstDate = new Date('2019-04-02 02:03:00')
const secondDate = new Date('2019-04-03 03:04:00')

const formatDate = dateObj => {
  const year = String(dateObj.getFullYear()).padStart(4, '0')
  const month = String(dateObj.getMonth() + 1).padStart(2, '0')
  const date = String(dateObj.getDate()).padStart(2, '0')
  const hour = String(dateObj.getHours()).padStart(2, '0')
  const minute = String(dateObj.getMinutes()).padStart(2, '0')
  const second = String(dateObj.getSeconds()).padStart(2, '0')

  return `${year}-${month}-${date} ${hour}:${minute}:${second}`
}

const res = []

const iteratedDate = new Date(firstDate.getTime())
while (iteratedDate <= secondDate) {
  res.push(formatDate(iteratedDate))
  iteratedDate.setMinutes(iteratedDate.getMinutes() + 1)
}

// res array is large so I sliced the first 10 amd the last 10
console.log(res.slice(0, 10))
console.log(res.slice(res.length - 10))

【讨论】:

    【解决方案3】:

    使用for 循环的稍微不同的方法;它还避免了为每分钟迭代创建一个新的 Date 实例。

    const p = new Date("2019-04-02T23:57:00");
    const q = new Date("2019-04-03T00:03:00");
    const r = [];
    for(const d = new Date(p); d <= q; d.setTime(d.getTime() + 60000))
    {
        r.push(d.toISOString().substring(0, 19).replace(/T/, " "));
    }
    const s = r.join("\n");
    console.log(s);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-11
      • 1970-01-01
      • 2018-11-07
      • 2018-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多