DEV Community

Akash Yadav
Akash Yadav

Posted on

Chronos

import Aphrodite from "./aphrodite";

class Chronos {
    ConvertUTCToLocal(timestamp) {
        const utcDate = new Date(timestamp);
        const localDate = new Date(utcDate.getTime() - (utcDate.getTimezoneOffset() * 60000));
        return localDate;
    }

    FormattedDateMinFromTimestamp(timestamp) {
        const date = this.ConvertUTCToLocal(timestamp);
        const localDate = date.toLocaleString(undefined, {
            year: 'numeric',
            month: 'short',
            day: 'numeric'
        });

        return localDate;
    }

    FormattedDateFromTimestamp(timestamp) {
        const date = this.ConvertUTCToLocal(timestamp);
        const localDate = date.toLocaleString(undefined, {
            year: 'numeric',
            month: 'long',
            day: 'numeric'
        });

        return localDate;
    }

    FormattedTimeFromTimestamp(timestamp) {
        const date = this.ConvertUTCToLocal(timestamp);
        const localTime = date.toLocaleString(undefined, {
            hour: 'numeric',
            minute: 'numeric',
            hour12: true
        });

        return localTime;
    }

    FormattedDateTimeFromTimestamp(timestamp) {
        const localDate = this.FormattedDateMinFromTimestamp(timestamp);
        const localTime = this.FormattedTimeFromTimestamp(timestamp);

        return `${localDate}, ${localTime}`;
    }

    FormattedDateDayFromTimestamp(timestamp) {
        const date = this.ConvertUTCToLocal(timestamp);
        const localDate = date.toLocaleString(undefined, {
            weekday: 'long',
            month: 'long',
            day: 'numeric'
        });

        return localDate;
    }

    IsSameDay(timestamp1, timestamp2) {
        const date1 = this.ConvertUTCToLocal(timestamp1);
        const date2 = this.ConvertUTCToLocal(timestamp2);

        return date1.getFullYear() === date2.getFullYear()
            && date1.getMonth() === date2.getMonth()
            && date1.getDate() === date2.getDate();
    }

    NamedDayOrDateFromTimestamp(timestamp) {
        const date = this.ConvertUTCToLocal(timestamp);
        const now = new Date();

        const elapsed = now.getTime() - date.getTime();

        const seconds = Math.floor(elapsed / 1000);
        const minutes = Math.floor(seconds / 60);
        const hours = Math.floor(minutes / 60);
        const days = Math.floor(hours / 24);

        if (days === 0) return 'Today';
        if (days === 1) return 'Yesterday';
        if (days < 7) return this.FormattedDateDayFromTimestamp(timestamp);

        return this.FormattedDateMinFromTimestamp(timestamp);
    }

    ElapsedTimeFromTimestamp(timestamp) {
        const date = this.ConvertUTCToLocal(timestamp);
        const now = new Date();

        const elapsed = now.getTime() - date.getTime();

        const seconds = Math.floor(elapsed / 1000);
        const minutes = Math.floor(seconds / 60);
        const hours = Math.floor(minutes / 60);
        const days = Math.floor(hours / 24);
        const months = Math.floor(days / 30);
        const years = Math.floor(months / 12);

        if (years > 0) return years === 1 ? 'A Year Ago' : `${Aphrodite.FormatToTwoDigits(years)} Years Ago`
        if (months > 0) return months === 1 ? 'A Month Ago' : `${Aphrodite.FormatToTwoDigits(months)} Months Ago`
        if (days > 0) return days === 1 ? 'A Day Ago' : `${Aphrodite.FormatToTwoDigits(days)} Days Ago`;
        if (hours > 0) return hours === 1 ? 'An Hour Ago' : `${Aphrodite.FormatToTwoDigits(hours)} Hours Ago`;
        if (minutes > 0) return minutes === 1 ? 'A Minute Ago' : `${Aphrodite.FormatToTwoDigits(minutes)} Minutes Ago`;

        return 'Just Now';
    }

    static SortObjectsByDate(objects, key = 'createddate', descending = true) {
        if (objects.length === 0 || objects.length === 1) return objects;
        return objects.sort((a, b) => descending ? new Date(b[key]) - new Date(a[key]) : new Date(a[key]) - new Date(b[key]));
    }
}

export default Chronos;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)