How to Make an Array of Times with Half Hour Intervals

How can I make an array of times with half hour intervals?

Thank-you for reopening the question alex.

This is a solution that should resonate with functional programmers.

function halfHourTimes() {
$formatter = function ($time) {
if ($time % 3600 == 0) {
return date('ga', $time);
} else {
return date('g:ia', $time);
}
};
$halfHourSteps = range(0, 47*1800, 1800);
return array_map($formatter, $halfHourSteps);
}

Generate array of times (as strings) for every X minutes in JavaScript

If the interval is only to be set in minutes[0-60], then evaluate the below solution w/o creating the date object and in single loop:

var x = 5; //minutes intervalvar times = []; // time arrayvar tt = 0; // start timevar ap = ['AM', 'PM']; // AM-PM
//loop to increment the time and push results in arrayfor (var i=0;tt<24*60; i++) { var hh = Math.floor(tt/60); // getting hours of day in 0-24 format var mm = (tt%60); // getting minutes of the hour in 0-55 format times[i] = ("0" + (hh % 12)).slice(-2) + ':' + ("0" + mm).slice(-2) + ap[Math.floor(hh/12)]; // pushing data in array in [00:00 - 12:00 AM/PM format] tt = tt + x;}
console.log(times);

How to create an array with hours and minutes in javascript?

If what you want is an array ["1:00", "1:15", ...] then why not just build that? It has nothing to do with "hours" and "minutes", only with "getting some obviously sequential numbers" right:

cost arr = [];
for (let i=0; i < 24; i++) {
for (let j=0; j < 4; j++) {
arr.push(`${i}:${j === 0 ? `00` : 15*j}`);
}
}

Done. Find your current time nearest a 15 minute block:

const d = new Date(),
h = d.getHours(),
m = 15 * Math.floor(d.getMinutes() / 15),
stamp = `${h}:${m === 0 ? `00` : m}`;

And just reorder the timeslots:

const pos = arr.indexOf(stamp);
let timelist = [];
if (pos > -1) {
timelist = [
...arr.slice(pos),
...arr.slice(0,pos)
];
}

Optimising sorting time in half an hour interval arrays

You could sort the array by using the ISO 8601 date string and take a hash table for grouping the same time slot.

var array = [{ lastAccessedTime: "2017-11-16T07:15:41Z", path: "/login.htm" }, { lastAccessedTime: "2017-11-16T07:32:42Z", path: "/login.htm" }, { lastAccessedTime: "2017-11-16T07:32:47Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T07:36:02Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T07:36:55Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T07:37:28Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T07:39:48Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T07:40:47Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T07:42:31Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T07:44:06Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T07:46:04Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T07:46:43Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T07:50:31Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T07:52:54Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T07:53:53Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T08:14:00Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T08:14:44Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T08:16:20Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T08:17:32Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T08:19:21Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T08:20:13Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T08:20:59Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T08:22:59Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T08:25:14Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T08:26:17Z", path: "/concurrent_users.htm" }, { id: 913, cookieId: "WfP2r2jiq4xAKo9YziaV5winTllwY5HL", creationTime: "2017-11-16T08:47:15Z", lastAccessedTime: "2017-11-16T08:47:15Z", path: "/login.htm" }, { lastAccessedTime: "2017-11-16T08:49:47Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:12:39Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:24:25Z", path: "/login.htm" }, { lastAccessedTime: "2017-11-16T09:26:15Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:28:10Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:28:53Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:30:49Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:31:48Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:34:01Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:43:47Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:44:21Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:44:30Z", path: "/login.htm" }, { lastAccessedTime: "2017-11-16T09:44:34Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:44:48Z", path: "/" }, { lastAccessedTime: "2017-11-16T09:44:54Z", path: "/login.htm" }, { lastAccessedTime: "2017-11-16T09:44:56Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:45:09Z", path: "/" }, { lastAccessedTime: "2017-11-16T09:45:14Z", path: "/login.htm" }, { lastAccessedTime: "2017-11-16T09:45:16Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:45:30Z", path: "/" }, { lastAccessedTime: "2017-11-16T09:45:35Z", path: "/login.htm" }, { lastAccessedTime: "2017-11-16T09:45:38Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:49:31Z", path: "/settings/license.htm" }, { lastAccessedTime: "2017-11-16T09:50:47Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:50:57Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:51:32Z", path: "/settings/license.htm" }, { lastAccessedTime: "2017-11-16T09:52:06Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:58:55Z", path: "/concurrent_users.htm" }, { lastAccessedTime: "2017-11-16T09:59:12Z", path: "/connections/qc.htm" }, { lastAccessedTime: "2017-11-16T09:59:22Z", path: "/connections/rally.htm" }, { lastAccessedTime: "2017-11-16T10:07:10Z", path: "/connections/rally.htm" }, { lastAccessedTime: "2017-11-16T10:11:21Z", path: "/connections/rally.htm" }, { lastAccessedTime: "2017-11-16T10:22:48Z", path: "/connections/rally.htm" }, { lastAccessedTime: "2017-11-16T10:30:27Z", path: "/connections/rally.htm" }, { lastAccessedTime: "2017-11-16T10:34:10Z", path: "/connections/rally.htm" }],    groups = Object.create(null),    result = [];
array.sort((a, b) => a.lastAccessedTime > b.lastAccessedTime || -(a.lastAccessedTime < b.lastAccessedTime));
array.forEach(function (o) { var slot = Math.floor(o.lastAccessedTime.slice(14, 16) / 30), key = o.lastAccessedTime.slice(0, 14) + (slot ? '30' : '00'); if (!groups[key]) { groups[key] = []; result.push(groups[key]); } groups[key].push(o);});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Javascript | Generate an array of times based on a start and an end

Looks like you could use Array.from and use the index to build the time string:

function getTimes(start, end) {
// Convert to number of half-hours
start = parseInt(start) * 2 + (+start.slice(-2) > 0);
end = parseInt(end) * 2 + (+end.slice(-2) > 0) + 1;
// Produce series
return Array.from({length: end - start}, (_, i) =>
(((i + start) >> 1) + ":" + ((i + start)%2*3) + "0").replace(/^\d:/, "0$&"));
};

console.log(getTimes("9:30", "22:30"));

Java - Create an array of times (15 minute) intervals between current time and a future set time

You're overwriting the time each cycle of the inner loop. You should use a List<String> instead and just append without worrying about indexes, like this:

String[] quarterHours = {"00","15","30","45"};
List<String> times = new ArrayList<String>; // <-- List instead of array

for(int i = 0; i < 24; i++) {
for(int j = 0; j < 4; j++) {
String time = i + ":" + quarterHours[j];
if(i < 10) {
time = "0" + time;
}
times.add("Today " + time); // <-- no need to care about indexes
}
}

Javascript Generate Array of Hours in the Day Starting with Current Hour

Pretty straight-forward. See comments inline:

var result = [];                      // Results will go herevar nowHour = new Date().getHours();  // Get current hour of the day
// Loop from current hour number to 23for(var i = nowHour; i < 24; i++){ result.push(i + "00"); // Put loop counter into array with "00" next to it}
console.log(result); // show results

How to create an hours + minutes array with moment.js and ES6?

function someFunction () {
const items = [];
new Array(24).fill().forEach((acc, index) => {
items.push(moment( {hour: index} ).format('h:mm A'));
items.push(moment({ hour: index, minute: 30 }).format('h:mm A'));
})
return items;
}


Related Topics



Leave a reply



Submit