V
V
veryoriginalnickname2021-07-16 20:48:38
JavaScript
veryoriginalnickname, 2021-07-16 20:48:38

Why do numbers decline strangely?

Data is coming from the server, and among this data is the date the record was last modified, in the form "2021-07-16T17:56:29.434+03:00". I convert this date to Date, and then decline. Like "edited 3 hours ago". The problem is that inclination works in a very strange way. If you refresh the page, then the same number is inclined differently. That "3 hours", then "3 hours", then "3 hours". Why?
Pieces of code:

<div class="article-item article-updated-at">ред. {{ convertDateWrap(article.updated_at) }}</div>

convertDateWrap(date){
      return ElvenTools.convertDate(date)
    }

const hoursTitles = ['час', 'часа', 'часов']
const minTitles = ['минуту', 'минуты', 'минут']
const secTitles = ['секунду', 'секунды', 'секунд']

class ElvenTools{
    public static convertDate(date){
        date = new Date(date)
        const currentDate = new Date()
        const isYesterday = currentDate.getFullYear() === date.getFullYear() && currentDate.getMonth() === date.getMonth() && currentDate.getDay() > date.getDay() && currentDate.getDay() - date.getDay() === 1
        const isToday = currentDate.getFullYear() === date.getFullYear() && currentDate.getMonth() === date.getMonth() && currentDate.getDay() === date.getDay()
        if(isYesterday){
            date = `вчера в ${date.getHours()}:${date.getMinutes()}`
        } else if(isToday){
            const secondsAgo = Math.round((currentDate.getTime() - date.getTime()) / 1000)
            const minutesAgo = Math.round(secondsAgo / 60)
            const hoursAgo = Math.round(minutesAgo / 60)
            if(secondsAgo < 60){
                // 1min-
                date = `${secondsAgo} ${this.declensionOfNumbers(secondsAgo, secTitles)} назад`
            } else if(minutesAgo < 60){
                // 1h-
                date = `${minutesAgo} ${this.declensionOfNumbers(secondsAgo, minTitles)} назад`
            } else if(hoursAgo === 1){
                // 2h-
                date = `час назад`
            } else if(hoursAgo < 8){
                // 8h-
                date = `${hoursAgo} ${this.declensionOfNumbers(secondsAgo, hoursTitles)} назад`
            } else if(hoursAgo < 24){
                // 24h-
                date = ${date.getHours()}:${date.getMinutes()}`
            }
        } else{
            date = `${date.getDay()}.${date.getMonth()}.${date.getFullYear()} в ${date.getHours()}:${date.getMinutes()}`
        }
        return date
    }

    public static declensionOfNumbers(number, titles){
        // https://gist.github.com/realmyst/1262561#gistcomment-3443551
        number = Math.abs(number);
        if (Number.isInteger(number)) {
            const cases = [2, 0, 1, 1, 1, 2];
            return titles[ (number%100>4 && number%100<20)? 2 : cases[(number%10<5)?number%10:5] ];
        }
        return titles[1]
    }
}

export default ElvenTools

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2021-07-16
@veryoriginalnickname

don't you notice anything?


// 8h-
date = `${hoursAgo} ${this.declensionOfNumbers( secondsAgo , hoursTitles)} ago`

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question