// JSON with a date string
const json = `{
"id": 42,
"createdAt": "2025-03-19T14:22:30.000Z"
}`;
// Define the expected shape of the parsed JSON
interface Payload {
id: number;
createdAt: string; // JSON always gives strings, not Date objects
}
// Parse the JSON into a typed object
const data: Payload = JSON.parse(json);
// Convert the ISO‑8601 string into a real Date instance
// JavaScript's Date constructor understands ISO strings natively
const JSONDate: Date = new Date(data.createdAt);
// Use it like any Date object
console.log(JSONDate.toISOString());
console.log(JSONDate.getFullYear());
console.log(JSONDate instanceof Date);
/*
run:
2025-03-19T14:22:30.000Z
2025
true
*/