-
Notifications
You must be signed in to change notification settings - Fork 68
Add week numbers #50
base: master
Are you sure you want to change the base?
Add week numbers #50
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
const ISO_DATE_FORMAT = /^(\d{4})-(\d{2})-(\d{2})$/ | ||
const MILLISECONDS_IN_WEEK = 604800000 | ||
|
||
export enum DaysOfWeek { | ||
Sunday = 0, | ||
|
@@ -209,3 +210,21 @@ export function chr4() { | |
export function createIdentifier(prefix) { | ||
return `${prefix}-${chr4()}${chr4()}-${chr4()}-${chr4()}-${chr4()}-${chr4()}${chr4()}${chr4()}` | ||
} | ||
|
||
export function weekOfYear(date: Date): number { | ||
const givenYear = date.getFullYear() | ||
const fourthOfJanuaryOfGivenYear = startOfWeek(new Date(givenYear, 0, 4)) | ||
const fourthOfJanuaryOfNextYear = startOfWeek(new Date(givenYear + 1, 0, 4)) | ||
let startYear | ||
if (date.getTime() >= fourthOfJanuaryOfNextYear.getTime()) { | ||
startYear = givenYear + 1 | ||
} else if (date.getTime() >= fourthOfJanuaryOfGivenYear.getTime()) { | ||
startYear = givenYear | ||
} else { | ||
startYear = givenYear - 1 | ||
} | ||
const start = startOfWeek(new Date(startYear, 0, 4)) | ||
const end = startOfWeek(date) | ||
const diff = end.getTime() - start.getTime() | ||
return Math.round(diff / MILLISECONDS_IN_WEEK) + 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm curious about the implementation of this. It's very hard to know if this gives correct results for every possibility. Is this lifted from somewhere (which is fine but a link in a comment would be useful), or have you written it yourself? Are you able to give a walk through of what's going on here? I'm curious about the use of 4th January. And also whether this would be affected by (and therefore should take as an argument) the first day of the week? I assume so There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here are the rules of how to determine week numbers https://en.wikipedia.org/wiki/Week#Week_numbering In my use-case iso week number made most sense as it matches values that PostgreSQL is returning( date-fns package getWeek function has two options - weekStartsOn and firstWeekContainsDate so solve this. How do you feel about adding momentjs or date-fns as a dependency? Or should we just reimplement the function here as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @WickyNilliams Have you had time to decide how this could be continued? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey sorry about the delay @krists. Have a lot going on at the moment, so it's hard to find time to dedicate to this. Oh that wikipedia article will be very useful. I started researching how to calculate this before and wasn't finding any good information. I don't want to add date-fns or moment as a dependency, since we were very focused on making this picker as lightweight as possible. I'll take a closer look at the logic here later in the week There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @WickyNilliams understandable. jquery-ui library has similar functionality as well. It provides a option to override default implemenation. |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you need
scope="row" role="rowheader"
here to properly indicate this is a header for the row.