mirror of
https://github.com/astuto/astuto.git
synced 2025-12-16 11:47:56 +01:00
Improve style pt. 4 (post and comments)
This commit is contained in:
33
app/javascript/helpers/friendlyDate.js
Normal file
33
app/javascript/helpers/friendlyDate.js
Normal file
@@ -0,0 +1,33 @@
|
||||
const friendlyDate = date => {
|
||||
var now = new Date();
|
||||
var timeStamp = fromRailsStringToJavascriptDate(date);
|
||||
|
||||
var secondsPast = (now.getTime() - timeStamp.getTime()) / 1000;
|
||||
|
||||
if (secondsPast < 60) {
|
||||
secondsPast = parseInt(secondsPast);
|
||||
return secondsPast + ' ' + (secondsPast === 1 ? 'second' : 'seconds') + ' ago';
|
||||
} else if (secondsPast < 3600) {
|
||||
let minutesPast = parseInt(secondsPast / 60);
|
||||
return minutesPast + ' ' + (minutesPast === 1 ? 'minute' : 'minutes') + ' ago';
|
||||
} else if (secondsPast <= 86400) {
|
||||
let hoursPast = parseInt(secondsPast / 3600);
|
||||
return hoursPast + ' ' + (hoursPast === 1 ? 'hour' : 'hours') + ' ago';
|
||||
} else {
|
||||
let daysPast = parseInt(secondsPast / 86400);
|
||||
return daysPast + ' ' + (daysPast === 1 ? 'day' : 'days') + ' ago';
|
||||
}
|
||||
}
|
||||
|
||||
export default friendlyDate;
|
||||
|
||||
/*
|
||||
Converts the default Rails datetime string
|
||||
format to a JavaScript Date object.
|
||||
*/
|
||||
const fromRailsStringToJavascriptDate = date => {
|
||||
let dateOnly = date.slice(0, 10);
|
||||
let timeOnly = date.slice(11, 19);
|
||||
|
||||
return new Date(`${dateOnly}T${timeOnly}Z`);
|
||||
}
|
||||
Reference in New Issue
Block a user