Skip to content

Commit 5e55cb9

Browse files
committed
Date range iterator
Signed-off-by: Alex Ivanov <ai@contributor.pw>
1 parent 10b90e7 commit 5e55cb9

File tree

8 files changed

+80
-36
lines changed

8 files changed

+80
-36
lines changed

snippets/common_js/datesRangeIterator/appsscript.json renamed to snippets/common_js/date-range-iterator/appsscript.json

File renamed without changes.
File renamed without changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* exported makeDatesRangeIterator */
2+
/**
3+
*
4+
* @param {Date} start
5+
* @param {Date} end
6+
* @param {number} step ms
7+
*/
8+
function* makeDatesRangeIterator_(start, end, step = 1000 * 60 * 60 * 24) {
9+
const _start = new Date(start);
10+
let _iterationCount = 0;
11+
while (_start <= end) {
12+
_iterationCount++;
13+
yield _start;
14+
_start.setTime(_start.getTime() + step);
15+
}
16+
return _iterationCount;
17+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
title: 'Date range iterator'
3+
date: '2021-04-19'
4+
description: 'Gets an iterator of dates for the specified range.'
5+
tags: ['js', 'common']
6+
categories: ['snippets']
7+
images: ['./snippets/sheets/unlink_urls/screenrecord.gif']
8+
---
9+
10+
{{< toc >}}
11+
12+
## Date range iterator
13+
14+
![Snippet of Date range iterator](./screenshot.png)
15+
16+
### Snippet
17+
18+
- {{< externalLink >}}
19+
- {{< commentLink >}}
20+
- {{< scrvizLink >}}
21+
22+
{{< codeFromFile "index.js" >}}
23+
24+
### Run it
25+
26+
{{< codeFromFile "run.js" >}}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* global makeDatesRangeIterator_ */
2+
3+
/**
4+
* Prints 100 days
5+
*/
6+
function runPrint100Days() {
7+
const start = new Date();
8+
const end = new Date();
9+
end.setTime(start.getTime() + 1000 * 60 * 60 * 24 * 100);
10+
11+
const it = makeDatesRangeIterator_(start, end);
12+
let result = it.next();
13+
while (!result.done) {
14+
console.log(result.value);
15+
result = it.next();
16+
}
17+
}
18+
19+
/**
20+
* Prints all mondays of the year
21+
*/
22+
function runPrintAllMondays() {
23+
const year = 2023;
24+
const start = new Date(year, 0, 1);
25+
const end = new Date(year, 12, 0);
26+
const step = 1000 * 60 * 60 * 24 * 7;
27+
28+
// Set the first monday
29+
while (start.getDay() !== 1) start.setDate(start.getDate() + 1);
30+
31+
const it = makeDatesRangeIterator_(start, end, step);
32+
let result = it.next();
33+
while (!result.done) {
34+
console.log(result.value);
35+
result = it.next();
36+
}
37+
}
50.6 KB
Loading
-141 KB
Binary file not shown.

snippets/common_js/datesRangeIterator/datesRangeIterator.js

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)