Use toLocaleDateString() or Intl.DateTimeFormat()
const date = new Date();
const formatted = date.toLocaleDateString('en-US');
console.log(formatted); // "12/31/2024"const now = new Date();
// Format for US locale
const usDate = now.toLocaleDateString('en-US');
// "12/31/2024"
// Format with time
const usDateTime = now.toLocaleString('en-US');
// "12/31/2024, 3:45:30 PM"
// Custom format
const custom = new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric'
}).format(now);
// "December 31, 2024"const iso = new Date().toISOString();
// "2024-12-31T15:45:30.000Z"const formatter = new Intl.RelativeTimeFormat('en');
formatter.format(-2, 'day'); // "2 days ago"Avoid toString() or manual string concatenation
// DON'T DO THIS
var day = date.getDate();
var month = date.getMonth() + 1;
var year = date.getFullYear();
var formatted = month + '/' + day + '/' + year;✓ Works in all modern browsers (ES5+)