Serialize
Try in Playground →Safe JSON date serialization and deserialization
Import
import { ... } from 'ts-time-utils/serialize'; Examples
JSON Serialization
Safely serialize and parse dates in JSON
import { serializeDate, parseJSONWithDates, stringifyWithDates } from 'ts-time-utils/serialize';
const date = new Date('2025-09-14T12:30:45.123Z');
// Serialize to different formats
console.log(serializeDate(date, { format: 'iso' }));
// "2025-09-14T12:30:45.123Z"
console.log(serializeDate(date, { format: 'epoch' }));
// 1757853045123
console.log(serializeDate(date, { format: 'date-only' }));
// "2025-09-14"
// Stringify objects with date fields
const data = {
id: 1,
name: 'Meeting',
createdAt: new Date(),
updatedAt: new Date(),
tags: ['important']
};
// Specify which fields are dates
const json = stringifyWithDates(data, ['createdAt', 'updatedAt']);
console.log(json);
// Parse JSON back with dates restored
const parsed = parseJSONWithDates(json, ['createdAt', 'updatedAt']);
console.log(parsed.createdAt instanceof Date); // true
console.log(parsed.updatedAt instanceof Date); // true