TIMEAGO

この関数を使用して、2つの時間の経過時間を計算します。

構文 引数 説明 time1 (必須) - 経過時間を計算する日時を表します。 TIMEAGO 関数をセルに直接入力したり、 setFormula メソッドを使用して数式を設定したりすることも出来ます。 メモ TIMEAGO関数は、ある時点から現在までの経過時間を簡単に計算するためのものです。 この機能はおもにプロジェクト管理で利用できます。 単純な日付ではなく、特定の日時との関連性を表したい場合は、TIMEAGO 関数を使用することができます。 メリット この機能により、複雑なカスタム関数やメソッドを作成することなく、2つの時間の経過時間を計算することができます。
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 "just now"; } 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 ? "in " + (years <= 1 ? "one year" : years + " years") : years <= 1 ? "one year ago" : years + " years ago"; } interval = seconds / 2592000; if (interval >= 1) { var 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) { var days = Math.floor(interval); return isFuture ? "in " + (days <= 1 ? "tomorrow" : days + " days") : days <= 1 ? "yesterday" : days + " days ago"; } interval = seconds / 3600; if (interval > 1) { var 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) { var 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"; }; 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="ja-jp" /> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/ja/purejs/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/ja/purejs/node_modules/@mescius/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/ja/purejs/node_modules/@mescius/spread-sheets-resources-ja/dist/gc.spread.sheets.resources.ja.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; }