Skip to content

Instantly share code, notes, and snippets.

@mbostock
Last active November 30, 2020 09:49
Show Gist options
  • 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;
})();
@vvkkishore123
Copy link

vvkkishore123 commented Feb 8, 2018

Hey Mike, sorry for posting on wrong thread. But my issue is related to d3.scaleLinear/d3.scaleTime. We are using D3 V4. Need to display date on x-axis and number on Y-axis. When set x-axis values to domain and specify ticks(5), I am facing 2 issues mentioned below.

  1. Sometimes ticks are more than 5 which I understood from D3 api that it is a limitation. This issue was addressed by using tickSize.
  2. But I am seeing dates on x-axis which are not in the input object. For example, I have dates 01/31, 02/01, 02/02, 02/05 and 02/06 (weekend dates were excluded), but x-axis is showing dates 02/03 and 02/04 which is not acceptable for my business.

Could you please us a direction to overcome this issue? Our organization just started using D3 so we have less help from our IT teams.

@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 [email protected]
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