TypeScript:
Converting a date into a string
How to:
// Simple conversion using toLocaleString()
let date = new Date();
let dateString = date.toLocaleString();
console.log(dateString); // "4/3/2023, 1:15:30 PM" (will vary based on locale)
// ISO format using toISOString()
let isoString = date.toISOString();
console.log(isoString); // "2023-04-03T13:15:30.000Z"
// Custom format using toLocaleDateString()
let customString = date.toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
});
console.log(customString); // "April 3, 2023"
Deep Dive
Think of a date’s string format as its passport, allowing it to travel across system boundaries - from databases to web pages. Historically, we’ve struggled with inconsistent date formats, which is why standards like ISO 8601 were introduced. This simplifies date exchange worldwide.
Alternatives to built-in methods? Libraries! Moment.js was the go-to for years, but these days date-fns or Luxon are the preferred choices - they’re lighter and more modular.
The essence of these conversions lies in the methods used. toLocaleString()
leans on the user’s locale, making it perfect for displaying to users. toISOString()
, however, stays true to the ISO 8601 format, which is brilliant for serializing and storing dates in a standard format. And toLocaleDateString()
gives you control over the appearance, catering to specific styling needs.