js How to get all dates from yearOfWeek or only accept next week dates?

23 Views Asked by At

In my app people can enter his work times for next week. So I want to prevent current dates or dates that are later then the next week. I only want to accept next week dates (or dates from a week year).

I found a function in dayjs where I can get the yearOfWeek

const dayjs = require('dayjs');

const weekOfYear = require('dayjs/plugin/weekOfYear');
dayjs.extend(weekOfYear);

const year = dayjs(new Date()).week();

In my app they are also a specific day limit where you can not anymore send your worker times until another week started.

All works but how can I only accept next week dates and not current dates or week that are later then next week ?

I found a way to prevent sending dates at specific time.

I have two variables:

let untilDayNumber = 5; // the number of the day in a week so 5 is friday at friday the user cannot send worker times anymore.
let untilDayHours = 21; // at how much o clock I prevent send the dates so when its friday but under 21 o clock then he can still send his times

I only need a way where I accept only dates from next week or from a specific calendar week.

CurrentCode:

const dayjs = require('dayjs');

const weekOfYear = require('dayjs/plugin/weekOfYear');
dayjs.extend(weekOfYear);

const weekOfYear = dayjs(new Date()).week();

let preventSendDay = 5;
let preventSendHours = 21;

// user worker times
let dates = [new Date(2024, 2, 8), new Date(2024, 2, 9), new Date(2024, 2, 10)];
const today = new Date();

const day = today.getDay() === 0 ? 7 : today.getDay(); // get day number

const hours = today.getHours(); // get day hours

// Now find a solution to accept only next week worker times


// prevent send worker times at specific day
if (
  day > preventSendDay ||
  (day === preventSendDay && hours >= preventSendHours)
) {
  console.log('PREVENT SENDING WORKER TIMES');
}

0

There are 0 best solutions below