Skip to main content

Date Transformers

Date transformers format validated dates for final output. They run after validation passes and typically change the data type (Date → String/Number).


All Transformers


ISO Format

toISOString()

Convert date to ISO string format

v.date().toISOString()
// Input: Date("2024-12-25") → Output: "2024-12-25T00:00:00.000Z"

Timestamp

toTimestamp()

Convert date to Unix timestamp (milliseconds)

v.date().toTimestamp()
// Input: Date("2024-12-25") → Output: 1735084800000

Custom Format

toFormat(format)

Convert date to specific format using dayjs

v.date().toFormat('YYYY-MM-DD')
// Input: Date("2024-12-25") → Output: "2024-12-25"

v.date().toFormat('DD/MM/YYYY')
// Input: Date("2024-12-25") → Output: "25/12/2024"

v.date().toFormat('YYYY-MM-DD HH:mm:ss')
// Input: Date("2024-12-25 14:30:00") → Output: "2024-12-25 14:30:00"

toDateOnly()

Convert to date only (remove time, returns YYYY-MM-DD)

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

toTimeOnly()

Convert to time only (returns HH:MM:SS)

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

Examples

ISO String Output

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

const result = await validate(dateSchema, '2024-12-25');
// Result: { isValid: true, data: "2024-12-25T00:00:00.000Z" }

Timestamp Output

const timestampSchema = v.date()
.required()
.fromToday()
.toTimestamp();

const result = await validate(timestampSchema, '2024-12-25');
// Result: { isValid: true, data: 1735084800000 }

Custom Format Output

const formattedSchema = v.date()
.required()
.fromToday()
.toFormat('YYYY-MM-DD');

const result = await validate(formattedSchema, '2024-12-25');
// Result: { isValid: true, data: "2024-12-25" }

Date Only Output

const dateOnlySchema = v.date()
.required()
.fromToday()
.toDateOnly();

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

Time Only Output

const timeOnlySchema = v.date()
.required()
.toTimeOnly();

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

Important Notes

Transformers are Final Output

Transformers change the data type and are typically used as the final step:

// ✅ Correct - Transformer as final output
v.date().required().fromToday().toISOString()

// ❌ Incorrect - Don't chain transformers
v.date().toISOString().toFormat('YYYY-MM-DD') // Won't work!

Order Matters

Mutators → Validators → Transformers

v.date()
.toStartOfDay() // Mutator: Transform date
.fromToday() // Validator: Check if valid
.toISOString() // Transformer: Format output

See Also