mirror of
https://github.com/astuto/astuto.git
synced 2025-12-16 11:47:56 +01:00
* It is now possible to follow a post in order to receive updates about it * Notifications are now sent when updates are published * Post status changes are now tracked * Update sidebar now shows the post status history * Mark a comment as a post update using the comment form * ... more ...
36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
export const friendlyDate = date => {
|
|
var now = new Date();
|
|
var timeStamp = fromRailsStringToJavascriptDate(date);
|
|
|
|
var secondsPast = (now.getTime() - timeStamp.getTime()) / 1000;
|
|
|
|
if (secondsPast < 60) {
|
|
return 'just now';
|
|
} else if (secondsPast < 3600) {
|
|
let minutesPast = Math.round(secondsPast / 60);
|
|
return minutesPast + ' ' + (minutesPast === 1 ? 'minute' : 'minutes') + ' ago';
|
|
} else if (secondsPast <= 86400) {
|
|
let hoursPast = Math.round(secondsPast / 3600);
|
|
return hoursPast + ' ' + (hoursPast === 1 ? 'hour' : 'hours') + ' ago';
|
|
} else {
|
|
let daysPast = Math.round(secondsPast / 86400);
|
|
return daysPast + ' ' + (daysPast === 1 ? 'day' : 'days') + ' ago';
|
|
}
|
|
}
|
|
|
|
export default friendlyDate;
|
|
|
|
/*
|
|
Converts the default Rails datetime string
|
|
format to a JavaScript Date object.
|
|
*/
|
|
export const fromRailsStringToJavascriptDate = date => {
|
|
let dateOnly = date.slice(0, 10);
|
|
let timeOnly = date.slice(11, 19);
|
|
|
|
return new Date(`${dateOnly}T${timeOnly}Z`);
|
|
}
|
|
|
|
export const fromJavascriptDateToRailsString = (date: Date) => {
|
|
return date.toJSON();
|
|
} |