index.umd.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  4. factory((global.SillyDatetime = {}));
  5. }(this, function (exports) { 'use strict';
  6. /**
  7. * 将输入的任意对象转换成 Date,如果装换失败将返回当前时间
  8. * @param {any} datetime 需要被格式化的时间
  9. * @return {Date} 转换好的 Date
  10. */
  11. function getDateObject(datetime) {
  12. var t = datetime instanceof Date ? datetime : new Date(datetime);
  13. if (!t.getDate()) {
  14. t = new Date();
  15. }
  16. return t;
  17. }
  18. /**
  19. * 格式化时间
  20. * @param {Date} datetime 需要被格式化的时间
  21. * @param {string} formatStr 格式化字符串,默认为 'YYYY-MM-DD HH:mm:ss'
  22. * @return {string} 格式化后的时间字符串
  23. */
  24. function format(datetime, formatStr) {
  25. var t = getDateObject(datetime);
  26. var hours = undefined,
  27. o = undefined,
  28. i = 0;
  29. formatStr = formatStr || 'YYYY-MM-DD HH:mm:ss';
  30. hours = t.getHours();
  31. o = [['M+', t.getMonth() + 1], ['D+', t.getDate()],
  32. // H 24小时制
  33. ['H+', hours],
  34. // h 12小时制
  35. ['h+', hours > 12 ? hours - 12 : hours], ['m+', t.getMinutes()], ['s+', t.getSeconds()]];
  36. // 替换 Y
  37. if (/(Y+)/.test(formatStr)) {
  38. formatStr = formatStr.replace(RegExp.$1, (t.getFullYear() + '').substr(4 - RegExp.$1.length));
  39. }
  40. // 替换 M, D, H, h, m, s
  41. for (; i < o.length; i++) {
  42. if (new RegExp('(' + o[i][0] + ')').test(formatStr)) {
  43. formatStr = formatStr.replace(RegExp.$1, RegExp.$1.length === 1 ? o[i][1] : ('00' + o[i][1]).substr(('' + o[i][1]).length));
  44. }
  45. }
  46. // 替换 a/A 为 am, pm
  47. return formatStr.replace(/a/ig, hours > 11 ? 'pm' : 'am');
  48. }
  49. /**
  50. * CONST and VAR for .fromNow
  51. */
  52. // 预设语言:英语
  53. var LOCALE_EN = {
  54. future: 'in %s',
  55. past: '%s ago',
  56. s: 'a few seconds',
  57. mm: '%s minutes',
  58. hh: '%s hours',
  59. dd: '%s days',
  60. MM: '%s months',
  61. yy: '%s years'
  62. };
  63. // 预设语言:简体中文
  64. var LOCALE_ZH_CN = {
  65. future: '%s内',
  66. past: '%s前',
  67. s: '几秒',
  68. mm: '%s分钟',
  69. hh: '%s小时',
  70. dd: '%s天',
  71. MM: '%s月',
  72. yy: '%s年'
  73. };
  74. // 当前本地化语言对象
  75. var _curentLocale = undefined;
  76. /**
  77. * 修改本地化语言
  78. * @param {string|Object} string: 预设语言 `zh-cn` 或 `en`;Object: 自定义 locate 对象
  79. */
  80. function locate(arg) {
  81. var newLocale = undefined,
  82. prop = undefined;
  83. if (typeof arg === 'string') {
  84. newLocale = arg === 'zh-cn' ? LOCALE_ZH_CN : LOCALE_EN;
  85. } else {
  86. newLocale = arg;
  87. }
  88. if (!_curentLocale) {
  89. _curentLocale = {};
  90. }
  91. for (prop in newLocale) {
  92. if (newLocale.hasOwnProperty(prop) && typeof newLocale[prop] === 'string') {
  93. _curentLocale[prop] = newLocale[prop];
  94. }
  95. }
  96. }
  97. /**
  98. * CONST for .fromNow
  99. */
  100. // 各计算区间
  101. var DET_STD = [['yy', 31536e6], // 1000 * 60 * 60 * 24 * 365 一年月按 365 天算
  102. ['MM', 2592e6], // 1000 * 60 * 60 * 24 * 30 一个月按 30 天算
  103. ['dd', 864e5], // 1000 * 60 * 60 * 24
  104. ['hh', 36e5], // 1000 * 60 * 60
  105. ['mm', 6e4], // 1000 * 60
  106. ['s', 0]];
  107. /**
  108. * 计算给出时间和当前时间的时间距离
  109. * @param {Date} datetime 需要计算的时间
  110. * @return {string} 时间距离
  111. */
  112. // 只要大于等于 0 都是秒
  113. function fromNow(datetime) {
  114. if (!_curentLocale) {
  115. // 初始化本地化语言为 en
  116. locate('');
  117. }
  118. var det = +new Date() - +getDateObject(datetime);
  119. var format = undefined,
  120. str = undefined,
  121. i = 0,
  122. detDef = undefined,
  123. detDefVal = undefined;
  124. if (det < 0) {
  125. format = _curentLocale.future;
  126. det = -det;
  127. } else {
  128. format = _curentLocale.past;
  129. }
  130. for (; i < DET_STD.length; i++) {
  131. detDef = DET_STD[i];
  132. detDefVal = detDef[1];
  133. if (det >= detDefVal) {
  134. str = _curentLocale[detDef[0]].replace('%s', parseInt(det / detDefVal, 0) || 1);
  135. break;
  136. }
  137. }
  138. return format.replace('%s', str);
  139. }
  140. exports.format = format;
  141. exports.locate = locate;
  142. exports.fromNow = fromNow;
  143. }));