Skip to main content

Date Mutators

Date mutators transform the input data before validation. All mutators are executed first, then validators run on the transformed data.


All Mutators


Time Adjustment

toStartOfDay()

Set to start of day (00:00:00.000)

v.date().toStartOfDay()
// Input: "2024-12-25 14:30:00" → Output: Date("2024-12-25 00:00:00.000")

toEndOfDay()

Set to end of day (23:59:59.999)

v.date().toEndOfDay()
// Input: "2024-12-25 14:30:00" → Output: Date("2024-12-25 23:59:59.999")

toUTC()

Convert date to UTC timezone

v.date().toUTC()
// Input: Date("2024-12-25 14:30:00") → Output: Date in UTC

Date Arithmetic

addDays(days)

Add or subtract days from date

v.date().addDays(7)     // Add 7 days
v.date().addDays(-7) // Subtract 7 days
// Input: "2024-12-25" → Output: Date("2025-01-01")

addMonths(months)

Add or subtract months from date

v.date().addMonths(1)   // Add 1 month
v.date().addMonths(-1) // Subtract 1 month
// Input: "2024-12-25" → Output: Date("2025-01-25")

addYears(years)

Add or subtract years from date

v.date().addYears(1)    // Add 1 year
v.date().addYears(-1) // Subtract 1 year
// Input: "2024-12-25" → Output: Date("2025-12-25")

addHours(hours)

Add or subtract hours from date

v.date().addHours(2)    // Add 2 hours
v.date().addHours(-2) // Subtract 2 hours
// Input: "2024-12-25 14:30:00" → Output: Date("2024-12-25 16:30:00")

Period Boundaries

toStartOfMonth()

Set to start of month (first day, 00:00:00)

v.date().toStartOfMonth()
// Input: "2024-12-25" → Output: Date("2024-12-01 00:00:00")

toEndOfMonth()

Set to end of month (last day, 23:59:59)

v.date().toEndOfMonth()
// Input: "2024-12-25" → Output: Date("2024-12-31 23:59:59")

toStartOfYear()

Set to start of year (January 1st, 00:00:00)

v.date().toStartOfYear()
// Input: "2024-12-25" → Output: Date("2024-01-01 00:00:00")

toEndOfYear()

Set to end of year (December 31st, 23:59:59)

v.date().toEndOfYear()
// Input: "2024-12-25" → Output: Date("2024-12-31 23:59:59")

Examples

Normalize to Start of Day

const dateSchema = v.date()
.required()
.toStartOfDay()
.fromToday();

const result = await validate(dateSchema, "2024-12-25 14:30:00");
// Result: { isValid: true, data: Date("2024-12-25 00:00:00") }

Add Days for Expiry Date

const expirySchema = v.date()
.required()
.toStartOfDay()
.addDays(30)
.fromToday();

const result = await validate(expirySchema, "2024-12-25");
// Result: { isValid: true, data: Date("2025-01-24 00:00:00") }

First Day of Next Month

const nextMonthSchema = v.date()
.required()
.toStartOfMonth()
.addMonths(1);

const result = await validate(nextMonthSchema, "2024-12-25");
// Result: { isValid: true, data: Date("2025-01-01 00:00:00") }

See Also