Generating all numbers between 2 dates saved as [day, hour]
I am given 2 dates, which is saved as 2 numbers [hour, weekday], where hour can be 0-23 and weekday can be 1-7. I am then trying to generate all the hours inbetween, such that e.g. [13, 2] and [2, 3] would generate:
[13,2]
[14,2]
[15,2]
...
[0,3]
[1,3]
[2,3]
I also have the 2 dates as datetimes, but I don't know if it is easier to use those. There is never more than a few days between the 2 dates, and they never cross the end of the week.
javascript
add a comment |
I am given 2 dates, which is saved as 2 numbers [hour, weekday], where hour can be 0-23 and weekday can be 1-7. I am then trying to generate all the hours inbetween, such that e.g. [13, 2] and [2, 3] would generate:
[13,2]
[14,2]
[15,2]
...
[0,3]
[1,3]
[2,3]
I also have the 2 dates as datetimes, but I don't know if it is easier to use those. There is never more than a few days between the 2 dates, and they never cross the end of the week.
javascript
add a comment |
I am given 2 dates, which is saved as 2 numbers [hour, weekday], where hour can be 0-23 and weekday can be 1-7. I am then trying to generate all the hours inbetween, such that e.g. [13, 2] and [2, 3] would generate:
[13,2]
[14,2]
[15,2]
...
[0,3]
[1,3]
[2,3]
I also have the 2 dates as datetimes, but I don't know if it is easier to use those. There is never more than a few days between the 2 dates, and they never cross the end of the week.
javascript
I am given 2 dates, which is saved as 2 numbers [hour, weekday], where hour can be 0-23 and weekday can be 1-7. I am then trying to generate all the hours inbetween, such that e.g. [13, 2] and [2, 3] would generate:
[13,2]
[14,2]
[15,2]
...
[0,3]
[1,3]
[2,3]
I also have the 2 dates as datetimes, but I don't know if it is easier to use those. There is never more than a few days between the 2 dates, and they never cross the end of the week.
javascript
javascript
asked Nov 25 '18 at 20:50
LindaLinda
13210
13210
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You can define a next
date function that relies on %
to select the next hour and day, and then you can generate all dates within a range with a simple while
loop:
function next([hour, day]) {
let nextHour = (hour + 1) % 24;
let nextDay = nextHour === 0 ? (day % 7) + 1 : day;
return [nextHour, nextDay];
}
function range([h1, d1], [h2, d2]) {
let res = , ch = h1, cd = d1;
while (ch !== h2 || cd !== d2) {
res.push([ch, cd]);
[ch, cd] = next([ch, cd]);
}
res.push([ch, cd]);
return res;
}
console.log(range([13, 2], [2, 3]));
console.log(range([13, 7], [10, 2]));
add a comment |
It is probably easier to use two Date
objects.
Something like:
var date1 = new Date(2018, 11, 20, 13);
var date2 = new Date(2018, 11, 21, 3);
var result = ;
while(date1.getTime() <= date2.getTime()) {
result.push(new Date(date1.getTime()))
date1.setHours(date1.getHours() + 1);
}
console.log(result)
add a comment |
You can do something like this:
const getDate = (h, d) => {
let date = new Date()
date.setHours(h)
date.setDate(date.getDate() - date.getDay() + d);
return date
}
const generateRange = (s, e) => {
let [startHour, startDay] = s, [endHour, endDay] = e, result =
startDate = getDate(startHour, startDay), endDate = getDate(endHour, endDay)
while (startDate < endDate) {
startDate.setHours(startDate.getHours() + 1)
result.push([startDate.getHours(), startDate.getDay()])
}
return result
}
console.log(generateRange([13, 2], [2, 3]))
console.log(generateRange([11, 1], [3, 2]))
The idea is to create the 2 dates and then in the while
loop just keep adding the hours until your start date is no longer less than the end date.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471836%2fgenerating-all-numbers-between-2-dates-saved-as-day-hour%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can define a next
date function that relies on %
to select the next hour and day, and then you can generate all dates within a range with a simple while
loop:
function next([hour, day]) {
let nextHour = (hour + 1) % 24;
let nextDay = nextHour === 0 ? (day % 7) + 1 : day;
return [nextHour, nextDay];
}
function range([h1, d1], [h2, d2]) {
let res = , ch = h1, cd = d1;
while (ch !== h2 || cd !== d2) {
res.push([ch, cd]);
[ch, cd] = next([ch, cd]);
}
res.push([ch, cd]);
return res;
}
console.log(range([13, 2], [2, 3]));
console.log(range([13, 7], [10, 2]));
add a comment |
You can define a next
date function that relies on %
to select the next hour and day, and then you can generate all dates within a range with a simple while
loop:
function next([hour, day]) {
let nextHour = (hour + 1) % 24;
let nextDay = nextHour === 0 ? (day % 7) + 1 : day;
return [nextHour, nextDay];
}
function range([h1, d1], [h2, d2]) {
let res = , ch = h1, cd = d1;
while (ch !== h2 || cd !== d2) {
res.push([ch, cd]);
[ch, cd] = next([ch, cd]);
}
res.push([ch, cd]);
return res;
}
console.log(range([13, 2], [2, 3]));
console.log(range([13, 7], [10, 2]));
add a comment |
You can define a next
date function that relies on %
to select the next hour and day, and then you can generate all dates within a range with a simple while
loop:
function next([hour, day]) {
let nextHour = (hour + 1) % 24;
let nextDay = nextHour === 0 ? (day % 7) + 1 : day;
return [nextHour, nextDay];
}
function range([h1, d1], [h2, d2]) {
let res = , ch = h1, cd = d1;
while (ch !== h2 || cd !== d2) {
res.push([ch, cd]);
[ch, cd] = next([ch, cd]);
}
res.push([ch, cd]);
return res;
}
console.log(range([13, 2], [2, 3]));
console.log(range([13, 7], [10, 2]));
You can define a next
date function that relies on %
to select the next hour and day, and then you can generate all dates within a range with a simple while
loop:
function next([hour, day]) {
let nextHour = (hour + 1) % 24;
let nextDay = nextHour === 0 ? (day % 7) + 1 : day;
return [nextHour, nextDay];
}
function range([h1, d1], [h2, d2]) {
let res = , ch = h1, cd = d1;
while (ch !== h2 || cd !== d2) {
res.push([ch, cd]);
[ch, cd] = next([ch, cd]);
}
res.push([ch, cd]);
return res;
}
console.log(range([13, 2], [2, 3]));
console.log(range([13, 7], [10, 2]));
function next([hour, day]) {
let nextHour = (hour + 1) % 24;
let nextDay = nextHour === 0 ? (day % 7) + 1 : day;
return [nextHour, nextDay];
}
function range([h1, d1], [h2, d2]) {
let res = , ch = h1, cd = d1;
while (ch !== h2 || cd !== d2) {
res.push([ch, cd]);
[ch, cd] = next([ch, cd]);
}
res.push([ch, cd]);
return res;
}
console.log(range([13, 2], [2, 3]));
console.log(range([13, 7], [10, 2]));
function next([hour, day]) {
let nextHour = (hour + 1) % 24;
let nextDay = nextHour === 0 ? (day % 7) + 1 : day;
return [nextHour, nextDay];
}
function range([h1, d1], [h2, d2]) {
let res = , ch = h1, cd = d1;
while (ch !== h2 || cd !== d2) {
res.push([ch, cd]);
[ch, cd] = next([ch, cd]);
}
res.push([ch, cd]);
return res;
}
console.log(range([13, 2], [2, 3]));
console.log(range([13, 7], [10, 2]));
edited Nov 25 '18 at 21:12
answered Nov 25 '18 at 21:03
sliderslider
8,46311231
8,46311231
add a comment |
add a comment |
It is probably easier to use two Date
objects.
Something like:
var date1 = new Date(2018, 11, 20, 13);
var date2 = new Date(2018, 11, 21, 3);
var result = ;
while(date1.getTime() <= date2.getTime()) {
result.push(new Date(date1.getTime()))
date1.setHours(date1.getHours() + 1);
}
console.log(result)
add a comment |
It is probably easier to use two Date
objects.
Something like:
var date1 = new Date(2018, 11, 20, 13);
var date2 = new Date(2018, 11, 21, 3);
var result = ;
while(date1.getTime() <= date2.getTime()) {
result.push(new Date(date1.getTime()))
date1.setHours(date1.getHours() + 1);
}
console.log(result)
add a comment |
It is probably easier to use two Date
objects.
Something like:
var date1 = new Date(2018, 11, 20, 13);
var date2 = new Date(2018, 11, 21, 3);
var result = ;
while(date1.getTime() <= date2.getTime()) {
result.push(new Date(date1.getTime()))
date1.setHours(date1.getHours() + 1);
}
console.log(result)
It is probably easier to use two Date
objects.
Something like:
var date1 = new Date(2018, 11, 20, 13);
var date2 = new Date(2018, 11, 21, 3);
var result = ;
while(date1.getTime() <= date2.getTime()) {
result.push(new Date(date1.getTime()))
date1.setHours(date1.getHours() + 1);
}
console.log(result)
var date1 = new Date(2018, 11, 20, 13);
var date2 = new Date(2018, 11, 21, 3);
var result = ;
while(date1.getTime() <= date2.getTime()) {
result.push(new Date(date1.getTime()))
date1.setHours(date1.getHours() + 1);
}
console.log(result)
var date1 = new Date(2018, 11, 20, 13);
var date2 = new Date(2018, 11, 21, 3);
var result = ;
while(date1.getTime() <= date2.getTime()) {
result.push(new Date(date1.getTime()))
date1.setHours(date1.getHours() + 1);
}
console.log(result)
answered Nov 25 '18 at 21:25
TitusTitus
14.4k11423
14.4k11423
add a comment |
add a comment |
You can do something like this:
const getDate = (h, d) => {
let date = new Date()
date.setHours(h)
date.setDate(date.getDate() - date.getDay() + d);
return date
}
const generateRange = (s, e) => {
let [startHour, startDay] = s, [endHour, endDay] = e, result =
startDate = getDate(startHour, startDay), endDate = getDate(endHour, endDay)
while (startDate < endDate) {
startDate.setHours(startDate.getHours() + 1)
result.push([startDate.getHours(), startDate.getDay()])
}
return result
}
console.log(generateRange([13, 2], [2, 3]))
console.log(generateRange([11, 1], [3, 2]))
The idea is to create the 2 dates and then in the while
loop just keep adding the hours until your start date is no longer less than the end date.
add a comment |
You can do something like this:
const getDate = (h, d) => {
let date = new Date()
date.setHours(h)
date.setDate(date.getDate() - date.getDay() + d);
return date
}
const generateRange = (s, e) => {
let [startHour, startDay] = s, [endHour, endDay] = e, result =
startDate = getDate(startHour, startDay), endDate = getDate(endHour, endDay)
while (startDate < endDate) {
startDate.setHours(startDate.getHours() + 1)
result.push([startDate.getHours(), startDate.getDay()])
}
return result
}
console.log(generateRange([13, 2], [2, 3]))
console.log(generateRange([11, 1], [3, 2]))
The idea is to create the 2 dates and then in the while
loop just keep adding the hours until your start date is no longer less than the end date.
add a comment |
You can do something like this:
const getDate = (h, d) => {
let date = new Date()
date.setHours(h)
date.setDate(date.getDate() - date.getDay() + d);
return date
}
const generateRange = (s, e) => {
let [startHour, startDay] = s, [endHour, endDay] = e, result =
startDate = getDate(startHour, startDay), endDate = getDate(endHour, endDay)
while (startDate < endDate) {
startDate.setHours(startDate.getHours() + 1)
result.push([startDate.getHours(), startDate.getDay()])
}
return result
}
console.log(generateRange([13, 2], [2, 3]))
console.log(generateRange([11, 1], [3, 2]))
The idea is to create the 2 dates and then in the while
loop just keep adding the hours until your start date is no longer less than the end date.
You can do something like this:
const getDate = (h, d) => {
let date = new Date()
date.setHours(h)
date.setDate(date.getDate() - date.getDay() + d);
return date
}
const generateRange = (s, e) => {
let [startHour, startDay] = s, [endHour, endDay] = e, result =
startDate = getDate(startHour, startDay), endDate = getDate(endHour, endDay)
while (startDate < endDate) {
startDate.setHours(startDate.getHours() + 1)
result.push([startDate.getHours(), startDate.getDay()])
}
return result
}
console.log(generateRange([13, 2], [2, 3]))
console.log(generateRange([11, 1], [3, 2]))
The idea is to create the 2 dates and then in the while
loop just keep adding the hours until your start date is no longer less than the end date.
const getDate = (h, d) => {
let date = new Date()
date.setHours(h)
date.setDate(date.getDate() - date.getDay() + d);
return date
}
const generateRange = (s, e) => {
let [startHour, startDay] = s, [endHour, endDay] = e, result =
startDate = getDate(startHour, startDay), endDate = getDate(endHour, endDay)
while (startDate < endDate) {
startDate.setHours(startDate.getHours() + 1)
result.push([startDate.getHours(), startDate.getDay()])
}
return result
}
console.log(generateRange([13, 2], [2, 3]))
console.log(generateRange([11, 1], [3, 2]))
const getDate = (h, d) => {
let date = new Date()
date.setHours(h)
date.setDate(date.getDate() - date.getDay() + d);
return date
}
const generateRange = (s, e) => {
let [startHour, startDay] = s, [endHour, endDay] = e, result =
startDate = getDate(startHour, startDay), endDate = getDate(endHour, endDay)
while (startDate < endDate) {
startDate.setHours(startDate.getHours() + 1)
result.push([startDate.getHours(), startDate.getDay()])
}
return result
}
console.log(generateRange([13, 2], [2, 3]))
console.log(generateRange([11, 1], [3, 2]))
edited Nov 25 '18 at 22:54
answered Nov 25 '18 at 22:27
AkrionAkrion
9,52011224
9,52011224
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471836%2fgenerating-all-numbers-between-2-dates-saved-as-day-hour%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown