Skip to content

Instantly share code, notes, and snippets.

@mbostock
Last active November 30, 2020 09:49
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mbostock/5827353 to your computer and use it in GitHub Desktop.
Save mbostock/5827353 to your computer and use it in GitHub Desktop.
Counting Weekdays
weekday = (function() {
// Returns the weekday number for the given date relative to January 1, 1970.
function weekday(date) {
var weekdays = weekdayOfYear(date),
year = date.getFullYear();
while (--year >= 1970) weekdays += weekdaysInYear(year);
return weekdays;
}
// Returns the date for the specified weekday number relative to January 1, 1970.
weekday.invert = function(weekdays) {
var year = 1970,
yearWeekdays;
// Compute the year.
while ((yearWeekdays = weekdaysInYear(year)) <= weekdays) {
++year;
weekdays -= yearWeekdays;
}
// Compute the date from the remaining weekdays.
var days = weekdays % 5,
day0 = ((new Date(year, 0, 1)).getDay() + 6) % 7;
if (day0 + days > 4) days += 2;
return new Date(year, 0, (weekdays / 5 | 0) * 7 + days + 1);
};
// Returns the number of weekdays in the specified year.
function weekdaysInYear(year) {
return weekdayOfYear(new Date(year, 11, 31)) + 1;
}
// Returns the weekday number for the given date relative to the start of the year.
function weekdayOfYear(date) {
var days = d3.time.dayOfYear(date),
weeks = days / 7 | 0,
day0 = (d3.time.year(date).getDay() + 6) % 7,
day1 = day0 + days - weeks * 7;
return Math.max(0, days - weeks * 2
- (day0 <= 5 && day1 >= 5 || day0 <= 12 && day1 >= 12) // extra saturday
- (day0 <= 6 && day1 >= 6 || day0 <= 13 && day1 >= 13)); // extra sunday
}
return weekday;
})();
@coresilver
Copy link

Hello,
It is awesome job but I cannot find a graph with Time frame such as H1, H4, D1, W1, M1.
Do you have plan to develop this sample ?
I need to know that How the Lib is support multiple time frame ?
And How we make UI tool overlay on graph area ?
if it able to support please let me know at coresilver@gmail.com
I have a project that need your help with good wage if you interest.
Thank you,
Rocky

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment