Skip to content
This repository has been archived by the owner on Jul 6, 2021. It is now read-only.
This repository has been archived by the owner on Jul 6, 2021. It is now read-only.

Shorter date format? #89

Open
la55u opened this issue Nov 5, 2018 · 2 comments
Open

Shorter date format? #89

la55u opened this issue Nov 5, 2018 · 2 comments

Comments

@la55u
Copy link

la55u commented Nov 5, 2018

Hey, new to vue here, I don't understand how to use the converter or converterOptions and there are no examples provided. What I try to achieve is to have a short formatted date like "1 min", "5 hours" etc. Could you help me how to do it with this package? Thanks

@sachinkrishna
Copy link

sachinkrishna commented Dec 11, 2018

Hello,
I am sorry if you have figured it out already, but if it helps - i just had a look in the example directory in this repo, I found this

<timeago :datetime="datetime2" :locale="locale" :autoUpdate="autoUpdate ? 1 : 0" :converterOptions="{ includeSeconds: true }" />

I have tested this and its working perfectly.

@Anima-t3d
Copy link

I wrote something basic:
<timeago :datetime="yourTime" :converter="convertTime" />

convertTime(date, locale, converterOptions) {
        /*console.log('date', date)
        console.log('locale', locale)
        console.log('converterOptions', converterOptions)*/

        const prefix = converterOptions.prefix || '';
        const suffix = converterOptions.suffix || 'ago';
        const yearSuffix = converterOptions.yearSuffix || 'y';
        const monthSuffix = converterOptions.monthSuffix || 'm';
        const daySuffix = converterOptions.daySuffix || 'd';
        const hourSuffix = converterOptions.hourSuffix || 'h';
        const minuteSuffix = converterOptions.minuteSuffix || 'min';
        const diffSpacer = converterOptions.diffSpacer || '';

        let difference = new Date().getTime() - new Date(Date.parse(date)).getTime();

        const yearsDifference = Math.floor(difference/1000/60/60/24/30/12);
        difference -= yearsDifference*1000*60*60*24*30*12;

        if (yearsDifference > 0) {
          return `${prefix} ${yearsDifference}${diffSpacer}${yearSuffix} ${suffix}`;
        }

        const monthsDifference = Math.floor(difference/1000/60/60/24/30);
        difference -= monthsDifference*1000*60*60*24*30;

        if (monthsDifference > 0) {
          return `${prefix} ${monthsDifference}${diffSpacer}${monthSuffix} ${suffix}`;
        }

        const daysDifference = Math.floor(difference/1000/60/60/24);
        difference -= daysDifference*1000*60*60*24;

        if (daysDifference > 0) {
          return `${prefix} ${daysDifference}${diffSpacer}${daySuffix} ${suffix}`;
        }

        const hoursDifference = Math.floor(difference/1000/60/60);
        difference -= hoursDifference*1000*60*60;

        if (hoursDifference > 0) {
          return `${prefix} ${hoursDifference}${diffSpacer}${hourSuffix} ${suffix}`;
        }

        const minutesDifference = Math.floor(difference/1000/60);
        difference -= minutesDifference*1000*60;

        if (minutesDifference > 0) {
          return `${prefix} ${minutesDifference}${diffSpacer}${minuteSuffix} ${suffix}`;
        }

        // console.log('difference', yearsDifference, monthsDifference, daysDifference, hoursDifference, minutesDifference)
        return `${prefix} less than a ${minuteSuffix} ${suffix}`
      },

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants