简体   繁体   English

在 javascript 中获取循环的 24 小时日期和时间

[英]get 24 hours date and time for loop in javascript

I have one drop-down(values are "Last 24 hours", "Last 48 hours", etc like) when I select "Last 24 hours" in the drop-down i need all dates from now to yesterday with one hour intervals.我有一个下拉列表(值是“过去 24 小时”、“过去 48 小时”等),当我在下拉列表中输入 select“过去 24 小时”时,我需要从现在到昨天的所有日期,间隔为一小时。

i tried this,我试过了,

   var todayDate = new Date();
    if(type=="hours"){ // for hours based drop-down
        var oneDayAgo = new Date(todayDate.getTime());
        oneDayAgo.setDate(todayDate.getDate() - 1);
        console.log("oneDayAgo"+oneDayAgo);
        var hours = todayDate.getHours();
        for(var i = hours; i <= hours+24; i++) {
            if(i<25){
                var newHours=i;
                var newDates=todayDate.getFullYear() + "-" + ("00" + (todayDate.getMonth() + 1)).slice(-2) + "-" + ("00" + todayDate.getDate()).slice(-2) + " " + ("00" +newHours).slice(-2) + ":" + ("00" + todayDate.getMinutes()).slice(-2) + ":" + ("00" + todayDate.getSeconds()).slice(-2);
                console.log(newDates);
            }else{
                var newHours=i-24;
                var newDates=oneDayAgo.getFullYear() + "-" + ("00" + (oneDayAgo.getMonth() + 1)).slice(-2) + "-" + ("00" + oneDayAgo.getDate()).slice(-2) + " " + ("00" +newHours).slice(-2) + ":" + ("00" + oneDayAgo.getMinutes()).slice(-2) + ":" + ("00" + oneDayAgo.getSeconds()).slice(-2);
                console.log(newDates);
            }
        }
    }

my expected output is,我预期的 output 是,

for example current date and time is 2014-04-27 13:07 means,例如当前日期和时间是 2014-04-27 13:07 意味着,

output like 2014-04-27 13:07, 2014-04-27 12:07, 2014-04-27 11:07, 2014-04-27 10:07.... 2014-04-26 13:07 output 喜欢 2014-04-27 13:07, 2014-04-27 12:07, 2014-04-27 11:07, 2014-04-27 10:07.... 2014-04-26 13:07

please help on this.请帮忙解决这个问题。 thanks谢谢

Here a working sample , which might be what you want. 这是一个工作样本 ,可能是你想要的。

//get type and hoursOption from dropdowns
var type = 'hours'
var hoursOption = 48;

var todayDate = new Date();

if(type=="hours"){ // for hours based drop-down

    var hours = todayDate.getHours();
    for(var i = hours; i <= hours + hoursOption; i++) {
            todayDate.setHours(todayDate.getHours() - 1)
        var newDates = todayDate.getFullYear() + "-" + ("00" + (todayDate.getMonth() + 1)).slice(-2) + "-" + ("00" + todayDate.getDate()).slice(-2) + " " + ("00" + todayDate.getHours()).slice(-2) + ":" + ("00" + todayDate.getMinutes()).slice(-2) + ":" + ("00" + todayDate.getSeconds()).slice(-2);
            console.log(newDates);

    }
}

For for-loop variable hours I added hourOption which comes from dropdown options, eg. 对于for-loop变量hours我添加了来自下拉选项的hourOption ,例如。 24, 48, 72, etc. 24,48,72等

Inside loop you take todayDate and just add one hour less then before, so it will show hours counting backwards. 在内部循环中你需要todayDate ,然后再减少一个小时,所以它将显示向后计数的小时数。

 function getDateItems(hours) { var toDate = new Date(); var fromDate = new Date(); fromDate.setTime(fromDate.getTime() - (hours * 60 * 60 * 1000)); var result = []; while (toDate >= fromDate) { result.push(toDate.getFullYear() + "-" + ("00" + (toDate.getMonth() + 1)).slice(-2) + "-" + ("00" + toDate.getDate()).slice(-2) + " " + ("00" + toDate.getHours()).slice(-2) + ":" + ("00" + toDate.getMinutes()).slice(-2) + ":" + ("00" + toDate.getSeconds()).slice(-2)); // consider using moment.js library to format date toDate.setTime(toDate.getTime() - (1 * 60 * 60 * 1000)); } return result; } var datesFrom24Hours = getDateItems(24); var datesFrom48Hours = getDateItems(48); console.log(datesFrom24Hours); 

This worked for me这对我有用

const start = new Date("2022-01-01T00:00:00");
    const end = new Date("2022-01-01T23:00:00");
    
    for (let current = start; current <= end; current.setHours(current.getHours() + 1)) {
        console.log(current);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM