<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ala GARBAA</title>
    <description>The latest articles on DEV Community by Ala GARBAA (@ala_garbaa).</description>
    <link>https://dev.to/ala_garbaa</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F751112%2Ff513038d-7596-4062-9653-95c04bb4f00c.jpg</url>
      <title>DEV Community: Ala GARBAA</title>
      <link>https://dev.to/ala_garbaa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ala_garbaa"/>
    <language>en</language>
    <item>
      <title>Format Time Ago Function - Time Differences in TypeScript</title>
      <dc:creator>Ala GARBAA</dc:creator>
      <pubDate>Sat, 03 Feb 2024 18:23:00 +0000</pubDate>
      <link>https://dev.to/ala_garbaa/format-time-ago-function-time-differences-in-typescript-5ag2</link>
      <guid>https://dev.to/ala_garbaa/format-time-ago-function-time-differences-in-typescript-5ag2</guid>
      <description>&lt;h1&gt;
  
  
  Function Explanation
&lt;/h1&gt;

&lt;p&gt;The following TypeScript function, &lt;code&gt;formatTimeAgo&lt;/code&gt;, is designed to format a given date or string into a human-readable "time ago" format. &lt;br&gt;
The function calculates the time difference between the provided date and the current date, presenting the result in terms of years, months, days, hours, minutes, or seconds.&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

export function formatTimeAgo(date: Date | string): string {

    // Initialize a default date as the current date
    let _date: Date = new Date();

    // Check if the input is a string and convert it to a Date object
    if (typeof date === "string") {
        _date = new Date(date)
    } else {
        _date = date
    }

    // Calculate the time difference in seconds
    const seconds: number = Math.floor((new Date().getTime() - _date.getTime()) / 1000);

    // Define intervals for different time units in seconds
    const intervals: Record&amp;lt;string, number&amp;gt; = {
        year: 31536000,
        month: 2628000,
        day: 86400,
        hour: 3600,
        minute: 60,
    };

    // Iterate through the intervals and determine the appropriate unit
    for (const [unit, secondsInUnit] of Object.entries(intervals)) {
        const interval: number = Math.floor(seconds / secondsInUnit);
        if (interval &amp;gt; 1) {
            return `${interval} ${unit}s ago`;
        } else if (interval === 1) {
            return `${interval} ${unit} ago`;
        }
    }

    // If no larger unit is found, return "just now"
    return "just now";
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>datetime</category>
      <category>formatting</category>
      <category>humanreadable</category>
    </item>
  </channel>
</rss>
