Explain Codes LogoExplain Codes Logo

Javascript - get the first day of the week from current date

javascript
date-manipulation
javascript-utility-functions
performance-optimization
Nikita BarsukovbyNikita Barsukov·Sep 6, 2024
TLDR

A quick fix: navigate to the start of the week with JavaScript's Date object:

const today = new Date(), // Don't panic, it's just today firstDayOfWeek = new Date(today.setDate(today.getDate() - today.getDay())); // Easy peasy

This provides the previous Sunday. For Monday as a starting point:

const today = new Date(); // Yup, still today const adjustDay = today.getDay() === 0 ? 6 : today.getDay() - 1; // Bye bye, Sunday const firstDayOfWeek = new Date(today.setDate(today.getDate() - adjustDay)); // Hello, sweet Monday!

Just alter the offset for today.getDay() to assign different week starting days.

Personalize your week

As unique as fingerprints, your week should start when you say so.

Assign your first day of the week

If you want to decide your week's starting day, do it your way:

function getFirstDayOfWeek(date, firstDay = 0) { // Default is zero but who's really counting? date = new Date(date); // New date, new you const dayOfWeek = date.getDay(); const firstDayDate = new Date(date.setDate(date.getDate() - (dayOfWeek - firstDay + 7) % 7)); firstDayDate.setHours(0, 0, 0, 0); // Up bright and early! return firstDayDate; } // Ready for the week? Pass 1 for Monday, 2 for Tuesday...

Considerate of time zones

Because not all time zones wake up at the same time:

// When crafted client-side, send as UTC time. It's hip to be square. const dateInUTC = new Date().toISOString();

Extra bells and whistles with libraries

Easy life with Date.js. Use the .previous().monday() method like a pro:

Date.today().previous().monday(); // Oh Monday, why do you always come after Sunday?

Visit Date.js if you want the week to start with an upper hand.

MongoDB's performance

For those JavaScript funcs in a MongoDB map function, strive for excellence.

MongoDB optimization

Find a balance between less object creation and direct date calc:

function getMonday(date) { var day = date.getDay(), // How's the day going? diff = date.getDate() - day + (day == 0 ? -6 : 1); // Fine-tuning the day return new Date(date.getFullYear(), date.getMonth(), diff); // Voila! Monday is served. } // Calling front-end devs using MongoDB map functions

Benchmarking to stay on top

Remember, benchmark your date operations on your MongoDB environment. Stay trendy!

Multi-case code handling

Get ready to juggle those advanced manipulations and specific cases.

Leap year wrangling

When working with February, leap year considerations are a must:

const year = new Date().getFullYear(); const isLeap = new Date(year, 1, 29).getMonth() === 1; // If 29th Feb is a reality, it's a Leap year

Daylight saving time acceptance

Daylight saving can be a curveball. Stay ahead with getTimezoneOffset:

const date = new Date(); const currentOffset = date.getTimezoneOffset(); // Sneaky, daylight saving, very sneaky

Reusability and readability - a lovestory

Future you and your team will appreciate readable and reusable script-ons. It's a team, remember?