index.js 3.7 KB

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