浏览代码

Add convenience date formatting functions

Steven Silvester 8 年之前
父节点
当前提交
08ab80cad8
共有 1 个文件被更改,包括 35 次插入0 次删除
  1. 35 0
      src/common/dates.ts

+ 35 - 0
src/common/dates.ts

@@ -0,0 +1,35 @@
+// Copyright (c) Jupyter Development Team.
+// Distributed under the terms of the Modified BSD License.
+
+import * as moment
+ from 'moment';
+
+
+/**
+ * Convert a timestring to a human readable string (e.g. 'two minutes ago').
+ *
+ * @param value - The date timestring.
+ *
+ * @returns A formatted date.
+ */
+export
+function humanTime(value: string): string {
+  let time = moment(value).fromNow();
+  time = time === 'a few seconds ago' ? 'seconds ago' : time;
+  return time;
+}
+
+
+/**
+ * Convert a timestring to a date format.
+ *
+ * @param value - The date timestring.
+ *
+ * @param format - The format string.
+ *
+ * @returns A formatted date.
+ */
+export
+function dateTime(value: string, format='YYYY-MM-DD HH:mm'): string {
+  return moment(value).format(format);
+}