Syntax
Argument
Description
time1
(Required) - shows the starting date and time from which we need to calculate elapsed time.
You can input the TIMEAGO function from the above code directly into a cell, or you can use the setFormula method to apply the formula.
Usage notes
The TIMEAGO function is meant to make it easier to calculate the time passed from a starting point to a current point in time
It's usage is mostly common on project management.
You can use the TIMEAGO function if you want to show something more relative to a specific time rather than a straightforward date.
Benefits
This function allows you to have a calculation of the interval passed between 2 moments without having to create complicated custom functions and methods.
<template>
<div class="sample-tutorial">
<gc-spread-sheets class="sample-spreadsheets" @workbookInitialized="initSpread">
<gc-worksheet :autoGenerateColumns='autoGenerateColumns'>
</gc-worksheet>
</gc-spread-sheets>
</div>
</template>
<script setup>
import '@mescius/spread-sheets-vue';
import { ref } from "vue";
import GC from "@mescius/spread-sheets";
const spreadRef = ref(null);
let initSpread = function (spread) {
spreadRef.value = spread;
//custom function - should be a genuine one
function TimeAgoFunction() {
this.name = "TIMEAGO";
this.maxArgs = 1;
this.minArgs = 1;
}
TimeAgoFunction.prototype = new GC.Spread.CalcEngine.Functions.Function();
TimeAgoFunction.prototype.evaluate = function (time1) {
switch (typeof time1) {
case 'number':
break;
case 'string':
time1 = +new Date(time1);
break;
case 'object':
if (time1.constructor === Date) time1 = time1.getTime();
break;
default:
time1 = +new Date();
}
let time2 = +new Date();
if (time1 == time2) {
return "just now";
}
let isFuture = (time2 < time1);
let seconds = (isFuture ? time1 - time2 : time2 - time1) / 1000;
let interval = seconds / 31536000;
if (interval >= 1) {
let years = Math.floor(interval);
return isFuture ? "in " + (years <= 1 ? "one year" : years + " years")
: years <= 1 ? "one year ago" : years + " years ago";
}
interval = seconds / 2592000;
if (interval >= 1) {
let months = Math.floor(interval);
return isFuture ? "in " + (months <= 1 ? "one month" : months + " months") : months <= 1 ? "one month ago" : months + " months ago";
}
interval = seconds / 86400;
if (interval >= 1) {
let days = Math.floor(interval);
return isFuture ? "in " + (days <= 1 ? "tomorrow" : days + " days")
: days <= 1 ? "yesterday" : days + " days ago";
}
interval = seconds / 3600;
if (interval > 1) {
let hours = Math.floor(interval);
return isFuture ? "in " + (hours <= 1 ? "an hour" : hours + " hours")
: hours <= 1 ? "an hour ago" : hours + " hours ago";
}
interval = seconds / 60;
if (interval > 1) {
let minutes = Math.floor(interval);
return isFuture ? "in " + (minutes <= 1 ? "a minute" : minutes + " minutes")
: minutes <= 1 ? "a minute ago" : minutes + " minutes ago";
}
seconds = Math.floor(seconds);
return isFuture ? "in " + (seconds <= 1 ? "one second" : seconds + " seconds")
: seconds <= 1 ? "one second ago" : seconds + " seconds ago";
};
spread.suspendPaint();
let sheet = spread.getSheet(0);
sheet.addCustomFunction(new TimeAgoFunction());
sheet.setColumnWidth(0, 200);
sheet.setColumnWidth(1, 200);
sheet.setColumnWidth(2, 150);
sheet.setValue(0, 0, 'Date');
sheet.setValue(0, 1, 'Formula');
sheet.setValue(0, 2, 'Result');
sheet.setFormula(1, 0, 'TODAY()');
sheet.setValue(1, 1, '=TIMEAGO(A2)');
sheet.setFormula(1, 2, 'TIMEAGO(A2)');
sheet.setValue(2, 0, new Date('2020-01-20'));
sheet.setValue(2, 1, '=TIMEAGO(A3)');
sheet.setFormula(2, 2, 'TIMEAGO(A3)');
sheet.setValue(3, 0, new Date('2021-02-10'));
sheet.setValue(3, 1, '=TIMEAGO(A4)');
sheet.setFormula(3, 2, 'TIMEAGO(A4)');
sheet.setValue(4, 0, new Date('2021-11-29 17:12:00'));
sheet.setValue(4, 1, '=TIMEAGO(A5)');
sheet.setFormula(4, 2, 'TIMEAGO(A5)');
spread.resumePaint();
}
let initExcel2010 = function (spread) {
let spreadNS = GC.Spread.Sheets;
let sheet = spread.getSheet(1);
sheet.name("Excel 2010 Functions");
sheet.setColumnWidth(0, 200);
sheet.setColumnWidth(1, 400);
sheet.setColumnWidth(2, 80);
sheet.getRange(-1, 0, -1, 1).wordWrap(true);
sheet.getRange(-1, 1, -1, 1).wordWrap(true);
let data = [
["Formula", "Description", "Result"],
["=BETA.DIST(2,8,10,TRUE,1,3)", "Cumulative beta probability density function"],
["=BETA.DIST(2,8,10,FALSE,1,3)", "Beta probability density function"],
["=CEILING.PRECISE(4.3)", "Rounds 4.3 up to the nearest multiple of 1."],
["=CEILING.PRECISE(-4.3)", "Rounds -4.3 up to the nearest multiple of 1. Rounds toward 0 because the number is negative."],
["=CEILING.PRECISE(4.3, 2)", "Rounds 4.3 up to the nearest multiple of 2."],
["=CHISQ.DIST(0.5,1,TRUE)", "The chi-squared distribution for 0.5, returned as the cumulative distribution function, using 1 degree of freedom."],
["=CHISQ.DIST(2,3,FALSE)", "The chi-squared distribution for 2, returned as the probability density function, using 3 degrees of freedom."],
["=CHISQ.INV(0.93,1)", "Inverse of the left-tailed probability of the chi-squared distribution for 0.93, using 1 degree of freedom."],
["=CHISQ.INV(0.6,2)", "Inverse of the left-tailed probability of the chi-squared distribution for 0.6, using 2 degrees of freedom."],
["=CONFIDENCE.T(0.05,1,50)", "Confidence interval for the mean of a population based on a sample size of 50, with a 5% significance level and a standard deviation of 1. This is based on a Student's t-distribution."],
["=COVARIANCE.S({2,4,8},{5,8,11})", "Sample covariance for the data points entered as an array in the function."],
["=ERF.PRECISE(0.74500)", "Error function integrated between 0 and 0.74500 (0.707929)"],
["=ERFC.PRECISE(0.74500)", "Complementary ERF function of 0.74500."],
["=F.DIST(15.2069,6,4,TRUE)", "F probability using the cumulative distribution function (TRUE cumulative argument)."],
["=F.DIST(15.2069,6,4,FALSE)", "F probability using the probability density function (FALSE cumulative argument)."],
["=F.INV(0.01,6,4)", "Inverse of the F probability distribution."],
["=FLOOR.PRECISE(-3.2)", "Rounds -3.2 down to the nearest multiple of -1"],
["=FLOOR.PRECISE(3.2)", "Rounds 3.2 down to the nearest multiple of 1"],
["=FLOOR.PRECISE(3.2, 2)", "Rounds 3.2 down to the nearest multiple of 2"],
["=GAMMALN.PRECISE(4)", "Natural logarithm of the gamma function at 4"],
["=HYPGEOM.DIST(1,4,8,20,TRUE)", "Cumulative hypergeometric distribution function."],
["=HYPGEOM.DIST(1,4,8,20,FALSE)", "Probability hypergeometric distribution function."],
["=ISO.CEILING(4.3)", "Rounds 4.3 up to nearest multiple of 1"],
["=ISO.CEILING(-4.3)", "Rounds -4.3 up to nearest multiple of 1"],
["=ISO.CEILING(4.3, 2)", "Rounds 4.3 up to the nearest multiple of 2"],
["=LOGNORM.DIST(4,3.5,1.2,TRUE)", "Cumulative lognormal distribution at 4."],
["=LOGNORM.DIST(4,3.5,1.2,FALSE)", "Probability lognormal distribution at 4."],
["=NEGBINOM.DIST(10,5,0.25,TRUE)", "Cumulative negative binomial distribution."],
["=NEGBINOM.DIST(10,5,0.25,FALSE)", "Probability negative binomial distribution."],
["=NETWORKDAYS.INTL(DATE(2006,1,1),DATE(2006,1,31))", "Results in 22 future workdays. Subtracts 9 nonworking weekend days (5 Saturdays and 4 Sundays) from the 31 total days between the two dates. By default, Saturday and Sunday are considered non-working days."],
["=NETWORKDAYS.INTL(DATE(2006,2,28),DATE(2006,1,31))", "Results in -21, which is 21 workdays in the past."],
["=NETWORKDAYS.INTL(DATE(2006,1,1),DATE(2006,2,1),7,{\"2006/1/2\",\"2006/1/16\"})", "Results in 22 future workdays by sutracting 10 nonworking days (4 Fridays, 4 Saturdays, 2 Holidays) from the 32 days between Jan 1 2006 and Feb 1 2006. Uses the 7 argument for weekend, which is Friday and Saturday. There are also two holidays in this time period."],
["=NETWORKDAYS.INTL(DATE(2006,1,1),DATE(2006,2,1),\"0010001\",{\"2006/1/2\",\"2006/1/16\"})", "Results in 20 future workdays. Same time period as above, but with Sunday and Wednesday as weekend days."],
["=NORM.S.DIST(1.333333,TRUE)", "Normal cumulative distribution function at 1.333333."],
["=NORM.S.DIST(1.333333,FALSE)", "Normal probability distribution function at 1.333333."],
["=PERCENTRANK.EXC({1,2,3,4,6,6,7,8,9}, 7)", "Returns the rank of the value 7 from the array."],
["=PERCENTRANK.EXC({1,2,3,4,6,6,7,8,9}, 5.43)", "Returns the rank of the value 5.43 in the array."],
["=PERCENTRANK.EXC({1,2,3,4,6,6,7,8,9}, 5.43, 1)", "Returns the rank of the value 5.43 in the array, displaying only 1 significant digit in the result (the default is 3)."],
["=PERCENTILE.EXC({1,2,3,4,5,6}, 0.25)", "Interpolates when the value for the specified percentile lies between two values in the array."],
["=QUARTILE.EXC({1,2,3,4,5,6,7,8,9,10,11},1)", "Locates the position of the first quartile (3)."],
["=QUARTILE.EXC({1,2,3,4,5,6,7,8,9,10,11},3)", "Locates the position of the third quartile (9)."],
["=RANK.AVG(95, {89, 88, 92, 101, 94, 97, 95})", "Finds the rank (the position) of the value 95 in the array (descending order). In this case, 95 was the 3rd one in descending order."],
["=RANK.AVG(95, {89, 88, 92, 101, 94, 97, 95}, 1)", "Finds the rank (the position) of the value 95 in the array (ascending order). In this case, 95 was the 5th one in ascending order."],
["=T.DIST(60,1,TRUE)", "Student's left-tailed t-distribution for 60, returned as the cumulative distribution function, using 1 degree of freedom."],
["=T.DIST(8,3,FALSE)", "Student's left-tailed t-distribution for 8, returned as the probability density function, using 3 degrees of freedom."],
["=T.INV(0.05464,60)", "The t-value of the Student's t-distribution based on specified arguments."],
["=WORKDAY.INTL(DATE(2012,1,1),30,0)", "Using a 0 for the Weekend argument results in a #NUM! error."],
["=WORKDAY.INTL(DATE(2012,1,1),90,11)", "Finds the date 90 workdays from 1/1/2012, counting only Sundays as a weekend day (Weekend argument is 11)."],
["=TEXT(WORKDAY.INTL(DATE(2012,1,1),30,17), \"m/dd/yyyy\")", "Uses the TEXT function to format the resulting serial number (40944) in a \"m/dd/yyyy\" format. Finds the date 30 workdays from 1/1/2012, counting only Saturdays as a weekend day (Weekend argument is 17)."]
];
sheet.setArray(0, 0, data, false);
let r, len, i;
for (r = 1, len = data.length; r < len; r++) {
sheet.setFormula(r, 2, data[r][0]);
}
let arrayFormulaData = [
["=MODE.MULT({1,2,3,4,3,2,1,2,1,3})", "The formula must be entered as an array formula. It returns 1, 2, and 3 as the modes because they each appear 3 times. If the formula is not entered as an array formula, the single result is 1.", 3]
];
for (i = 0, len = arrayFormulaData.length; i < len; i++) {
let cur = arrayFormulaData[i],
rows = cur[2],
cols = cur[3] || 1;
sheet.addSpan(r, 0, rows, 1);
sheet.setValue(r, 0, cur[0]);
sheet.addSpan(r, 1, rows, 1);
sheet.setValue(r, 1, cur[1]);
sheet.setArrayFormula(r, 2, rows, cols, cur[0]);
r += rows;
}
for (i = 1; i < r; i++) {
sheet.autoFitRow(i);
}
sheet.getRange(-1, 2, -1, 1).formatter(".######");
sheet.setFormatter(48, 2, "M/d/yyyy");
let table = sheet.tables.add("FunctionTable", 0, 0, 50, 3, spreadNS.Tables.TableThemes.medium9);
table.rowFilter().filterButtonVisible(false);
// Array formula with merge cells, set style like above table rows
let rowStyle = sheet.getActualStyle(48, 0);
sheet.getRange(50, 0, 3, 3).backColor(rowStyle.backColor);
sheet.getRange(49, 0, 4, 3).setBorder(rowStyle.borderBottom, {
all: true
});
}
</script>
<style scoped>
#app {
height: 100%;
}
.sample-tutorial {
position: relative;
height: 100%;
overflow: hidden;
}
.sample-spreadsheets {
width: 100%;
height: 100%;
overflow: hidden;
float: left;
}
.options-container {
float: right;
width: 280px;
padding: 12px;
height: 100%;
box-sizing: border-box;
background: #fbfbfb;
overflow: auto;
}
.option-row {
font-size: 14px;
padding: 5px;
margin-top: 10px;
}
#switchAutoMergeMode {
margin: 10px 0px;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
</style>
<!DOCTYPE html>
<html style="height:100%;font-size:14px;">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>SpreadJS VUE</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css"
href="$DEMOROOT$/en/vue3/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/en/vue3/node_modules/systemjs/dist/system.src.js"></script>
<script src="./systemjs.config.js"></script>
<script src="./compiler.js" type="module"></script>
<script>
var System = SystemJS;
System.import("./src/app.js");
System.import('$DEMOROOT$/en/lib/vue3/license.js');
</script>
</head>
<body>
<div id="app"></div>
</body>
</html>
(function (global) {
SystemJS.config({
transpiler: 'plugin-babel',
babelOptions: {
es2015: true
},
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
packageConfigPaths: [
'./node_modules/*/package.json',
"./node_modules/@mescius/*/package.json",
"./node_modules/@babel/*/package.json",
"./node_modules/@vue/*/package.json"
],
map: {
'vue': "npm:vue/dist/vue.esm-browser.js",
'tiny-emitter': 'npm:tiny-emitter/index.js',
'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js',
"systemjs-babel-build": "npm:systemjs-plugin-babel/systemjs-babel-browser.js",
'@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js',
'@mescius/spread-sheets-resources-en': 'npm:@mescius/spread-sheets-resources-en/index.js',
'@mescius/spread-sheets-vue': 'npm:@mescius/spread-sheets-vue/index.js'
},
meta: {
'*.css': { loader: 'systemjs-plugin-css' },
'*.vue': { loader: "../plugin-vue/index.js" }
}
});
})(this);