Browse Source

Calculate and return current week number

arvid-e 5 months ago
parent
commit
cb637a35d2
1 changed files with 22 additions and 2 deletions
  1. 22 2
      apps/app/src/server/routes/contributions.ts

+ 22 - 2
apps/app/src/server/routes/contributions.ts

@@ -2,7 +2,27 @@
 // not sure where to put the utility functions
 // not sure where to put the utility functions
 // so put them here temporarely
 // so put them here temporarely
 
 
-export const getCurrentWeekNumber = (date: Date) => {
-  const d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
+
+export const getISOWeekNumber = (date: Date) => {
+  const utcDate = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
+
+  const dayNum = utcDate.getUTCDay() || 7;
+
+  utcDate.setUTCDate(utcDate.getUTCDate() + 4 - dayNum);
+
+  const yearStart = new Date(Date.UTC(utcDate.getUTCFullYear(), 0, 1));
+
+  const oneDayInMs = 86400000; // 1000 * 60 * 60 * 24
+
+  const diffInMs = utcDate.getTime() - yearStart.getTime();
+
+  const weekNo = Math.ceil(((diffInMs / oneDayInMs) + 1) / 7);
+
+  return weekNo;
+};
+
+
+export const daysSinceLastUpdate = (lastUpdateDate: Date, currentDate: Date = new Date()) => {
+
 
 
 };
 };