Example 1 — ISO string
Reliable parse.
JavaScript
console.log(new Date("2020-01-02T00:00:00.000Z").toISOString());How It Works
- Prefer ISO-8601.
Call new Date() for now, new Date(ms) for a timestamp, or new Date(string) to parse (prefer ISO strings). Avoid relying on non-ISO parse formats.
const now = new Date();
console.log(now instanceof Date);
const epoch = new Date(0);
console.log(epoch.toISOString());Reliable parse.
console.log(new Date("2020-01-02T00:00:00.000Z").toISOString());Month is 0-based.
const d = new Date(2020, 0, 2);
console.log(d.getFullYear(), d.getMonth(), d.getDate());CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
6 people found this page helpful