Extracting, Adding, and Formatting Days with date-fns
Date Object
"date-fns" primarily works with standard JavaScript Date objects. These objects contain information about a specific date and time.Extracting Day Information
Several functions in "date-fns" allow you to extract the day information from a Date object. These functions return a number representing the day of the month (1-31). Here are some examples:getDate(date)
: This function returns the day of the month for a given date object.
Manipulating Days
"date-fns" provides functions to manipulate dates by specific day increments. For instance:addDays(date, amount)
: This function adds a specified number of days to a date object and returns a new Date object.
Formatting with Day
While not a specific type, "date-fns" offers formatting functions that can include the day information in the output. For example:format(date, 'dd/MM/yyyy')
: This function formats the date object as "day/month/year".
In summary
- Functions exist to extract day information, manipulate dates by days, and format dates including the day.
- "date-fns" doesn't have a dedicated "Day" type, but it works with JavaScript Date objects.
Extracting Day of the Month
const today = new Date();
// Get the day of the month (1-31)
const day = dateFns.getDate(today);
console.log("Day of the month:", day);
Adding or Subtracting Days
const vacationStart = new Date(2024, 5, 10); // June 10th, 2024
// Add 3 days to the vacation start date
const vacationEnd = dateFns.addDays(vacationStart, 3);
console.log("Vacation end date:", vacationEnd);
// Subtract 7 days from today
const oneWeekAgo = dateFns.subDays(new Date(), 7);
console.log("One week ago:", oneWeekAgo);
const upcomingMeeting = new Date(2024, 5, 28); // May 28th, 2024
// Format the date with day, month, and year
const formattedDate = dateFns.format(upcomingMeeting, "EEEE, MMMM do, yyyy");
console.log("Formatted meeting date:", formattedDate); // Output: Tuesday, May 28th, 2024
Native JavaScript Date Object
const today = new Date();
// Get day of the month
const day = today.getDate();
console.log("Day of the month:", day);
// Add 3 days
const vacationStart = new Date(2024, 5, 10);
vacationStart.setDate(vacationStart.getDate() + 3);
console.log("Vacation end date:", vacationStart);
// Format with day, month, and year (requires additional manipulation)
const upcomingMeeting = new Date(2024, 5, 28);
const options = { weekday: 'long', month: 'long', day: 'numeric', year: 'numeric' };
const formattedDate = upcomingMeeting.toLocaleDateString('en-US', options);
console.log("Formatted meeting date:", formattedDate);
Moment.js Library
Moment.js is another popular library for date and time manipulation in JavaScript. It offers a rich set of features similar to "date-fns" with a slightly different syntax. You'd need to include the Moment.js library in your project to use it.
Third-party Libraries
Several other libraries like Luxon or Day.js provide alternative functionalities for date and time manipulation in JavaScript. Explore these options if "date-fns" doesn't suit your specific needs.