2

I need to display a whole week from Monday To Sunday in following format "Week 2: 05.01 - 11.01" as an example

let d = new Date();
let date = d.getDate();
let day = d.getDay();
let currentWeek = Math.ceil((date + 6 - day) / 7);

this is what i have for the current week, i know that it will have some issues if the previous month is in the first week of the current month.

Any help as to how to make it accurate and reliable?

I need it to work in React Native!

clockingClock
  • 31
  • 1
  • 3

1 Answers1

0

I like using moment to deal with Dates, since date operations are somewhat complex considering month changes, leap years and all that.

Using moment, you'd just need to do

const today = moment();
const begginingOfCurrentWeek = today.startOf('week');
const endOfWeek = today.endOf('week);

I havent tested it, but it should be something along those lines. You can also format dates easily using moment().format('DD/MM/YY'), for example.

Here's the link to their documentation: https://momentjs.com/

Thales Kenne
  • 2,705
  • 1
  • 12
  • 26