Lesson 17 of 55 · javascript
Working with Dates – `Date` object & Intl
Duration: 8 mins
Create a date:
const now = new Date();
const specific = new Date('2024-02-27T10:00:00Z');
Common methods:
getFullYear(),getMonth(),getDate()– retrieve parts.toISOString(),toLocaleString()– format output.
Intl.DateTimeFormat for locale‑aware formatting:
const fmt = new Intl.DateTimeFormat('de-DE', { dateStyle:'short', timeStyle:'short' });
console.log(fmt.format(now)); // 27.02.24, 11:00
Example: Countdown timer
const end = new Date('2024-12-31');
setInterval(()=>{
const diff = end - new Date();
if(diff<=0){ clearInterval(this); console.log('Happy New Year!'); }
else{ console.log(Math.floor(diff/1000)+' seconds left'); }
},1000);