TIMEAGO

使用这个函数可以计算起点到当前时间点的时间。

语法 参数 描述 time1 (必须) - 起始时间,即我们需要计算时间长度的起始时间。 您可以像上面的示例一样直接在一个单元格内输入TIMEAGO公式,或者您可以使用 setFormula方法来应用这个公式。 使用说明 TIMEAGO函数旨在简化计算从起点到当前时间点的时间。 它的用法通常在项目管理中很常见。 如果您想显示更多与特定时间相关的信息,而不是简单的日期,则可以使用 TIMEAGO 函数。 优点 此函数使您无需计算复杂的自定义函数和方法即可计算两个时刻之间的间隔。
window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(document.getElementById('ss')); initSpread(spread); }; function initSpread(spread) { var sheet = spread.getActiveSheet(); //this should be a SpreadJS Function not a custom 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(); } var time2 = +new Date(); if (time1 == time2) { return "刚刚"; } var isFuture = (time2 < time1); var seconds = (isFuture ? time1 - time2 : time2 - time1) / 1000; var interval = seconds / 31536000; if (interval >= 1) { var years = Math.floor(interval); return isFuture ? "在 " + (years <= 1 ? "一年" : years + " 年") + "后" : years <= 1 ? "一年前" : years + " 年前"; } interval = seconds / 2592000; if (interval >= 1) { var months = Math.floor(interval); return isFuture ? "在 " + (months <= 1 ? "一个月" : months + " 个月") + "后" : months <= 1 ? "一个月前" : months + " 个月前"; } interval = seconds / 86400; if (interval >= 1) { var days = Math.floor(interval); return isFuture ? "在 " + (days <= 1 ? "明天" : days + " 天") + "后" : days <= 1 ? "昨天" : days + " 天前"; } interval = seconds / 3600; if (interval > 1) { var hours = Math.floor(interval); return isFuture ? "在 " + (hours <= 1 ? "一小时" : hours + " 小时") + "后" : hours <= 1 ? "一小时前" : hours + " 小时前"; } interval = seconds / 60; if (interval > 1) { var minutes = Math.floor(interval); return isFuture ? "在 " + (minutes <= 1 ? "一分钟" : minutes + " 分钟") + "后" : minutes <= 1 ? "一分钟前" : minutes + " 分钟前"; } seconds = Math.floor(seconds); return isFuture ? "在 " + (seconds <= 1 ? "一秒" : seconds + " 秒") + "后" : seconds <= 1 ? "一秒前" : seconds + " 秒前"; }; var timeAgo = new TimeAgoFunction(); sheet.addCustomFunction(timeAgo); // spread.fromJSON(data[0]); sheet.setColumnWidth(0, 200); sheet.setColumnWidth(1, 200); sheet.setColumnWidth(2, 150); sheet.setValue(0, 0, '日期'); sheet.setValue(0, 1, '公式'); sheet.setValue(0, 2, '结果'); 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)'); }
<!doctype html> <html style="height:100%;font-size:14px;"> <head> <meta name="spreadjs culture" content="zh-cn" /> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-resources-zh/dist/gc.spread.sheets.resources.zh.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/spread/source/js/license.js" type="text/javascript"></script> <script src="app.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="styles.css"> </head> <body> <div class="sample-tutorial"> <div id="ss" class="sample-spreadsheets"></div> </div> </body> </html>
.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; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }