Last active
March 6, 2023 15:00
-
-
Save SnisarOnline/5b8493aea5e6c5eae4f49343c6804aac to your computer and use it in GitHub Desktop.
грубый подсчет времени после публикации
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Pipe, PipeTransform} from '@angular/core'; | |
/** | |
* Фильтер Расчет прошедшего времени от времени публикации, | |
* @Example <div>{{news.time | past_time}} ago</div> | |
* @param {number} create_post время создания поста | |
* @returns {string} Сколько времени прошло с момента создания поста | |
*/ | |
@Pipe({ | |
name: 'past_time' | |
}) | |
export class PastTimePipe implements PipeTransform { | |
transform(create_post) { | |
// Set the unit values in milliseconds. | |
const msecPerMinute = 1000 * 60; // милисекунд в минуте | |
const msecPerHour = msecPerMinute * 60; // милисекунд в часе | |
const msecPerDay = msecPerHour * 24; // милисекунд в дне | |
const msecPerWeek = msecPerDay * 7; // милисекунд в неделе | |
const msecPerMonth = msecPerDay * 30; // милисекунд в месяце | |
const msecPerYear = msecPerMonth * 12; // милисекунд в году | |
// Set a date and get the milliseconds | |
const date = new Date(); // сегоднешняя дата | |
// console.log(date.getTime()); // тайм стемп сегодня | |
const dateMsec = create_post; // дата создания поста | |
// Get the difference in milliseconds., Интервал между созданием поста и текущей датой | |
let interval = date.getTime() - dateMsec; | |
// Calculate how many days the interval contains. Subtract that | |
// many days from the interval to determine the remainder. | |
const year = Math.floor(interval / msecPerYear); | |
interval = interval - (year * msecPerYear ); | |
const month = Math.floor(interval / msecPerMonth); | |
interval = interval - (month * msecPerMonth ); | |
const week = Math.floor(interval / msecPerWeek); | |
interval = interval - (week * msecPerWeek ); | |
const days = Math.floor(interval / msecPerDay); | |
interval = interval - (days * msecPerDay ); | |
// Calculate the hours, minutes, and seconds. | |
const hours = Math.floor(interval / msecPerHour); | |
interval = interval - (hours * msecPerHour ); | |
const minutes = Math.floor(interval / msecPerMinute); | |
interval = interval - (minutes * msecPerMinute ); | |
const seconds = Math.floor(interval / 1000); | |
// вернем все | |
// return year + ' год; ' + month + ' месяцей; ' + week + ' недель; ' + days + ' days, ' + hours + ' hours, ' + minutes + ' minutes, ' + seconds + ' seconds.' ; | |
// вернем что-то 1 если уже прошел 1 год, то нам ненужно остальное | |
if (year !== 0) { | |
return this.add_s(year, " year"); | |
} | |
if (month !== 0) { | |
return this.add_s(month, " month"); | |
} | |
if (week !== 0) { | |
return this.add_s(week, " week"); | |
} | |
if (days !== 0) { | |
return this.add_s(days, " day"); | |
} | |
if (hours !== 0) { | |
return this.add_s(hours, " hour"); | |
} | |
if (minutes !== 0) { | |
return this.add_s(minutes, " min"); | |
} | |
} | |
add_s(data_number: number, text: string) { | |
switch (data_number) { | |
case 1: | |
return data_number + text; | |
default: | |
return data_number + text + "s"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment