JavaScript Date Formats in TypeScript

When it comes to handling dates in JavaScript, understanding various date formats is essential for creating dynamic and user-friendly applications. The JavaScript Date object empowers developers to work with dates, and this guide explores three common date formats: ISO Date, Short Date, and Long Date. Additionally, we'll showcase examples in both ES8 and TypeScript, providing a comprehensive overview of modern approaches.

ISO Date. The International Standard

The ISO 8601 format (YYYY-MM-DD) is an international standard widely adopted for its clarity and precision. Using the JavaScript Date object, we can easily work with ISO dates.

Example 1. Basic ISO Date

<!DOCTYPE html>
<html>
<head></head>
<body>
  <div>
    <h1>Welcome to the JavaScript Date Formats Guide</h1>
    <h3>Example 1: Basic ISO Date</h3>
    <p id="isoExample"></p>
  </div>
  <script>
    const isoDate = new Date("2020-08-01");
    document.getElementById("isoExample").innerHTML = isoDate;
  </script>
</body>
</html>

In this example, the ISO date "2020-08-01" is displayed in the output relative to the current time zone.

Additional ISO Date Formats

// Complete date format using ISO date
new Date("2020-08-01");

// Year and month (YYYY-MM) without day
new Date("2020-08");

// Only the year (YYYY) without month and day
new Date("2020");

// Date with added hours, minutes, and seconds (YYYY-MM-DDTHH:MM:SSZ)
new Date("2020-08-01T07:05:00Z");

These variations allow us to specify different levels of detail in ISO date formats.

Short Date. MM/DD/YYYY Format

The Short Date format is represented as "MM/DD/YYYY." It provides a concise way to display dates.

Example 2. Short Date

<!DOCTYPE html>
<html>
<head></head>
<body>
  <div>
    <h1>Welcome to the JavaScript Date Formats Guide</h1>
    <h3>Example 2: Short Date</h3>
    <p id="shortDateExample"></p>
  </div>
  <script>
    const shortDate = new Date("08/01/2020");
    document.getElementById("shortDateExample").innerHTML = shortDate;
  </script>
</body>
</html>

In this example, the Short Date "08/01/2020" is displayed.

Long Date. MMM DD YYYY Format

The Long Date format is represented as "MMM DD YYYY." It allows flexibility in writing months and dates.

Example 3. Long Date

<!DOCTYPE html>
<html>
<head></head>
<body>
  <div>
    <h1>Welcome to the JavaScript Date Formats Guide</h1>
    <h3>Example 3: Long Date</h3>
    <p id="longDateExample"></p>
  </div>
  <script>
    const longDate = new Date("Aug 01 2020");
    document.getElementById("longDateExample").innerHTML = longDate;
  </script>
</body>
</html>

In this example, the Long Date "Aug 01, 2020" is displayed, showcasing the flexibility in writing months.

ES8 RelativeTimeFormat. A Modern Approach

ES8 introduced the Intl.RelativeTimeFormat improved formatting of relative time.

Example 4. ES8 RelativeTimeFormat

<!DOCTYPE html>
<html>
<head></head>
<body>
  <div>
    <h1>Welcome to the JavaScript Date Formats Guide</h1>
    <h3>Example 4: ES8 RelativeTimeFormat</h3>
    <p id="es8Example"></p>
  </div>
  <script>
    const es8Date = new Date("2020-08-01");
    const diffInDays = new Intl.RelativeTimeFormat('en', { numeric: 'auto' }).format((Date.now() - es8Date) / (1000 * 60 * 60 * 24), 'day');
    document.getElementById("es8Example").innerHTML = `Days ago: ${diffInDays}`;
  </script>
</body>
</html>

In this example, we use ES8s Intl.RelativeTimeFormat to display the relative time difference in days.

TypeScript. Strong Typing for Dates

TypeScript adds a layer of static typing to JavaScript, ensuring code robustness. Here's an example of working with dates in TypeScript.

Example 5. TypeScript Date

<!DOCTYPE html>
<html>
<head></head>
<body>
  <div>
    <h1>Welcome to the JavaScript Date Formats Guide</h1>
    <h3>Example 5: TypeScript Date</h3>
    <p id="typescriptExample"></p>
  </div>
  <script>
    const currentDate: Date = new Date();
    document.getElementById("typescriptExample").innerHTML = `Current Date: ${currentDate.toDateString()}`;
  </script>
</body>
</html>

In this TypeScript example, we declare a variable currentDate of type Date and display it in the output.

This comprehensive guide covered three common date formats: ISO Date, Short Date, and Long Date, providing practical examples in both ES8 and TypeScript. The ISO 8601 format stands out as an international standard, offering clarity and precision. Through various syntaxes, developers can represent complete dates, year and month combinations, or even dates with added time components. Short Date format ("MM/DD/YYYY") provides a concise representation suitable for many applications, while Long Date format ("MMM DD YYYY") offers flexibility in writing months and dates.

ES8 introduced the Intl.RelativeTimeFormat, enabling developers to express time differences in a human-readable manner. This modern approach enhances the user experience by displaying relative time, such as "X days ago." Finally, TypeScript brings strong typing to date handling, ensuring code robustness and reducing runtime errors. Developers can leverage TypeScript's static typing to declare and manipulate dates with confidence. Whether you're working with traditional JavaScript, exploring the features of ES8, or embracing TypeScript for type safety, this guide equips you with the knowledge to master date formats and create dynamic, user-friendly applications. As you continue your journey in web development, incorporating these date-handling techniques will enhance the precision and clarity of your applications.


Similar Articles