day()
Set the day of the week with the day() method. Learn how to manipulate dates to specific days using standard day values (0-6, Sunday-Saturday), preserve time components during changes, and implement common patterns like scheduling recurring events and finding specific weekdays.
Sets the day of the week. Returns a new instance (immutable).
Syntax
.day(value: number): FduInstanceParameters
value: number- Day of week (0-6, where 0 = Sunday, 6 = Saturday)
Returns
FduInstance - New instance with the updated day of week
Examples
Setting Day of Week
import { fdu } from "@pyyupsk/fdu";
const date = fdu("2025-10-06"); // Monday
date.day(0); // Previous/next Sunday: 2025-10-05
date.day(1); // Stays Monday: 2025-10-06
date.day(5); // Next Friday: 2025-10-10
date.day(6); // Next Saturday: 2025-10-11Setting to Weekend
const monday = fdu("2025-10-06");
const saturday = monday.day(6); // 2025-10-11
const sunday = monday.day(0); // 2025-10-05Preserving Time
const date = fdu("2025-10-06T14:30:45"); // Monday 2:30 PM
const friday = date.day(5);
friday.format("YYYY-MM-DD HH:mm:ss"); // '2025-10-10 14:30:45'Day Values
| Value | Day |
|---|---|
| 0 | Sunday |
| 1 | Monday |
| 2 | Tuesday |
| 3 | Wednesday |
| 4 | Thursday |
| 5 | Friday |
| 6 | Saturday |
Behavior
The method calculates the difference between the current day and target day, then adjusts the date accordingly:
const wednesday = fdu("2025-10-08"); // Wednesday (day 3)
wednesday.day(0); // 2025-10-05 (Sunday, -3 days)
wednesday.day(6); // 2025-10-11 (Saturday, +3 days)
wednesday.day(3); // 2025-10-08 (same day, no change)Immutability
.day() returns a new instance and never modifies the original:
const original = fdu("2025-10-06"); // Monday
const modified = original.day(5); // Friday
original.format("dddd"); // 'Monday' (unchanged)
modified.format("dddd"); // 'Friday'Common Patterns
Find next occurrence of a day
function nextDay(date: FduInstance, targetDay: number): FduInstance {
const result = date.day(targetDay);
return result.isBefore(date) ? result.add(7, "day") : result;
}
const monday = fdu("2025-10-06");
const nextFriday = nextDay(monday, 5); // 2025-10-10Schedule weekly meeting
const today = fdu();
const nextMeeting = today.day(3).add(9, "hour"); // Next Wednesday at 9 AM
console.log(`Meeting: ${nextMeeting.format("YYYY-MM-DD HH:mm")}`);Get start/end of work week
const date = fdu("2025-10-08"); // Wednesday
const workWeekStart = date.day(1); // Monday
const workWeekEnd = date.day(5); // Friday
console.log(
`Week: ${workWeekStart.format("MMM D")} - ${workWeekEnd.format("MMM D")}`,
);Find last occurrence of a day
function lastDay(date: FduInstance, targetDay: number): FduInstance {
const result = date.day(targetDay);
return result.isAfter(date) ? result.subtract(7, "day") : result;
}
const friday = fdu("2025-10-10");
const lastMonday = lastDay(friday, 1); // 2025-10-06See Also
- day() getter - Get day of the week
- weekday() - Locale-aware day setting
- add() - Add time to a date
- subtract() - Subtract time from a date
subtract()
Learn to use the subtract() method to subtract years, months, weeks, days, hours, minutes, seconds, and milliseconds from dates. Master immutable date manipulation, chaining operations, handling month/year edge cases, and calculating historical dates, time ranges, and deadlines with practical examples.
weekday()
Set locale-aware day of the week with the weekday() method. Learn how to manipulate dates using locale-specific week starts (Sunday vs Monday), handle different cultural week conventions, and implement international scheduling patterns with proper locale support.