我想在每个启动时间中添加15分钟的插槽,然后存储在一系列对象中

发布于 2025-02-01 04:44:08 字数 121 浏览 4 评论 0原文

 const createTimeSlots=(fromTime,toTime)=>{
      

我想在循环中为每个启动时间添加15分钟的插槽,然后存储在一系列对象中。

 const createTimeSlots=(fromTime,toTime)=>{
      

I want to add 15 minutes slot to each StartTime in a loop and store in array of objects.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

于我来说 2025-02-08 04:44:08

假设输入在时间戳中,则添加15分钟的时间戳,然后推动时间戳(或推动分钟/小时等)。这是启动时间是当前时间戳,末端时间是时间戳中的当前 + 3小时。

   function createSlots(start, end) {
    let slots = [];
    const mins = 15 * 60 * 1000; // 15 mins

    const date = (dt) => new Date(dt);

    while (start <= end) {
        start += mins;

        // only mins
        //slots.push(date(start).getMinutes());

        // hrs +  mins
        slots.push(`${date(start).getHours()}:${date(start).getMinutes()}`);
    }
    return slots;
}

var slots = createSlots(Date.now(), Date.now() + 3 * 3600 * 1000); // from (now) to (now + 3hrs)
console.log("slots : ", slots);

Assuming the inputs are in timestamp, add 15 mins equivalent of timestamps and push that timestamp(or push mins/hrs etc.). Here's the code example where start time is current timestamp and endtime is current + 3hrs in timestamp.

   function createSlots(start, end) {
    let slots = [];
    const mins = 15 * 60 * 1000; // 15 mins

    const date = (dt) => new Date(dt);

    while (start <= end) {
        start += mins;

        // only mins
        //slots.push(date(start).getMinutes());

        // hrs +  mins
        slots.push(`${date(start).getHours()}:${date(start).getMinutes()}`);
    }
    return slots;
}

var slots = createSlots(Date.now(), Date.now() + 3 * 3600 * 1000); // from (now) to (now + 3hrs)
console.log("slots : ", slots);

塔塔猫 2025-02-08 04:44:08

假设输入是有效的日期时间格式。
该解决方案将跨日期工作,假设您今天和明天结束时间的开始时间,那么它也可以无问题。

const createTimeSlots = (fromTime, toTime, slotLength =15*60) => {
    let slotStart = new Date(fromTime).valueOf();
    let slotEnd = new Date(fromTime).valueOf() + slotLength * 1000;
    let endEpoch = new Date(toTime).valueOf();
    let ob = [];
    for (slotEnd; slotEnd <= endEpoch; slotEnd = slotEnd + slotLength * 1000) {
        ob.push({
            'from': formatDate(slotStart),
            'to': formatDate(slotEnd)
        });
        slotStart = slotEnd;
    }
    return ob;
}

function formatDate(epoch) {
    let d = new Date(epoch);
    let month = String((d.getMonth() + 1)).padStart(2, '0');
    let day = String((d.getDate())).padStart(2, '0');
    let hours = String((d.getHours())).padStart(2, '0');
    let mins = String((d.getMinutes())).padStart(2, '0');

    return `${d.getFullYear()}-${month}-${day} ${hours}:${mins}`;
}

const from = "2022-05-25 23:00";
const to = "2022-05-26 01:00";
const slotLength = 15 * 60; //seconds
var r = createTimeSlots(from, to, slotLength );
console.log(r);

Let's assume inputs are valid date-time format.
This solution will work across dates, let's say you give the start time today and end time tomorrow then also it will work without any issue.

const createTimeSlots = (fromTime, toTime, slotLength =15*60) => {
    let slotStart = new Date(fromTime).valueOf();
    let slotEnd = new Date(fromTime).valueOf() + slotLength * 1000;
    let endEpoch = new Date(toTime).valueOf();
    let ob = [];
    for (slotEnd; slotEnd <= endEpoch; slotEnd = slotEnd + slotLength * 1000) {
        ob.push({
            'from': formatDate(slotStart),
            'to': formatDate(slotEnd)
        });
        slotStart = slotEnd;
    }
    return ob;
}

function formatDate(epoch) {
    let d = new Date(epoch);
    let month = String((d.getMonth() + 1)).padStart(2, '0');
    let day = String((d.getDate())).padStart(2, '0');
    let hours = String((d.getHours())).padStart(2, '0');
    let mins = String((d.getMinutes())).padStart(2, '0');

    return `${d.getFullYear()}-${month}-${day} ${hours}:${mins}`;
}

const from = "2022-05-25 23:00";
const to = "2022-05-26 01:00";
const slotLength = 15 * 60; //seconds
var r = createTimeSlots(from, to, slotLength );
console.log(r);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文