Query
Master date querying methods to extract individual components from date instances. Learn how to get year, month, day, hour, minute, second, and millisecond values with practical examples for checking weekends, validating dates, building time objects, and implementing common date patterns.
Query methods allow you to extract individual components from a date instance.
year()
Gets the 4-digit year value.
.year(): numberExample
import { fdu } from '@pyyupsk/fdu';
const date = fdu('2024-01-15');
date.year(); // 2024month()
Gets the month value (0-11, where 0 = January).
.month(): numberMonths are zero-indexed like JavaScript's native Date object. January is 0, February is 1, etc.
Example
const date = fdu('2024-01-15');
date.month(); // 0 (January)
const date2 = fdu('2024-12-25');
date2.month(); // 11 (December)date()
Gets the day of the month (1-31).
.date(): numberExample
const date = fdu('2024-01-15');
date.date(); // 15
const date2 = fdu('2024-01-01');
date2.date(); // 1day()
Gets the day of the week (0-6, where 0 = Sunday).
.day(): numberExample
const date = fdu('2024-01-15'); // Monday
date.day(); // 1
const sunday = fdu('2024-01-14');
sunday.day(); // 0
const saturday = fdu('2024-01-20');
saturday.day(); // 6Day Values
| Value | Day |
|---|---|
| 0 | Sunday |
| 1 | Monday |
| 2 | Tuesday |
| 3 | Wednesday |
| 4 | Thursday |
| 5 | Friday |
| 6 | Saturday |
hour()
Gets the hour in 24-hour format (0-23).
.hour(): numberExample
const date = fdu('2024-01-15T14:30:00');
date.hour(); // 14
const midnight = fdu('2024-01-15T00:00:00');
midnight.hour(); // 0
const noon = fdu('2024-01-15T12:00:00');
noon.hour(); // 12minute()
Gets the minutes (0-59).
.minute(): numberExample
const date = fdu('2024-01-15T14:30:00');
date.minute(); // 30
const date2 = fdu('2024-01-15T14:05:00');
date2.minute(); // 5second()
Gets the seconds (0-59).
.second(): numberExample
const date = fdu('2024-01-15T14:30:45');
date.second(); // 45
const date2 = fdu('2024-01-15T14:30:08');
date2.second(); // 8millisecond()
Gets the milliseconds (0-999).
.millisecond(): numberExample
const date = fdu('2024-01-15T14:30:45.123');
date.millisecond(); // 123
const date2 = fdu('2024-01-15T14:30:45.005');
date2.millisecond(); // 5Complete Example
import { fdu } from '@pyyupsk/fdu';
const date = fdu('2024-01-15T14:30:45.123');
console.log(date.year()); // 2024
console.log(date.month()); // 0 (January)
console.log(date.date()); // 15
console.log(date.day()); // 1 (Monday, 0=Sunday)
console.log(date.hour()); // 14
console.log(date.minute()); // 30
console.log(date.second()); // 45
console.log(date.millisecond()); // 123Common Patterns
Check if weekend
const date = fdu('2024-01-15');
const day = date.day();
const isWeekend = day === 0 || day === 6; // Sunday or SaturdayCheck if specific day of week
const date = fdu('2024-01-15');
const isMonday = date.day() === 1;
const isFriday = date.day() === 5;Check if morning/afternoon/evening
const date = fdu('2024-01-15T14:30:00');
const hour = date.hour();
const isMorning = hour >= 6 && hour < 12;
const isAfternoon = hour >= 12 && hour < 18;
const isEvening = hour >= 18 && hour < 22;
const isNight = hour >= 22 || hour < 6;Check if specific month
const date = fdu('2024-12-25');
const isDecember = date.month() === 11; // Remember: 0-indexed!
const isJanuary = date.month() === 0;Extract time components
const date = fdu('2024-01-15T14:30:45.123');
const timeObj = {
hours: date.hour(),
minutes: date.minute(),
seconds: date.second(),
milliseconds: date.millisecond()
};
// { hours: 14, minutes: 30, seconds: 45, milliseconds: 123 }Build date object
const date = fdu('2024-01-15T14:30:45');
const dateObj = {
year: date.year(),
month: date.month() + 1, // Convert to 1-12
day: date.date(),
dayOfWeek: date.day(),
hour: date.hour(),
minute: date.minute(),
second: date.second()
};See Also
diff()
Learn how to calculate differences between dates using the diff() method. Calculate age, duration, time elapsed, and date differences in years, months, weeks, days, hours, minutes, seconds, and milliseconds with practical examples for deadline warnings, relative time, working hours, and subscription duration tracking.
toDate()
Learn how to convert fdu instances to native JavaScript Date objects using the toDate() method. Integrate seamlessly with APIs, date pickers, chart libraries, calendar components, and third-party tools that require standard JavaScript Date objects with practical integration examples.