util.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. function formatTime(time) {
  2. if (typeof time !== 'number' || time < 0) {
  3. return time
  4. }
  5. var hour = parseInt(time / 3600)
  6. time = time % 3600
  7. var minute = parseInt(time / 60)
  8. time = time % 60
  9. var second = time
  10. return ([hour, minute, second]).map(function (n) {
  11. n = n.toString()
  12. return n[1] ? n : '0' + n
  13. }).join(':')
  14. }
  15. function formatLocation(longitude, latitude) {
  16. if (typeof longitude === 'string' && typeof latitude === 'string') {
  17. longitude = parseFloat(longitude)
  18. latitude = parseFloat(latitude)
  19. }
  20. longitude = longitude.toFixed(2)
  21. latitude = latitude.toFixed(2)
  22. return {
  23. longitude: longitude.toString().split('.'),
  24. latitude: latitude.toString().split('.')
  25. }
  26. }
  27. var dateUtils = {
  28. UNITS: {
  29. '年': 31557600000,
  30. '月': 2629800000,
  31. '天': 86400000,
  32. '小时': 3600000,
  33. '分钟': 60000,
  34. '秒': 1000
  35. },
  36. humanize: function (milliseconds) {
  37. var humanize = '';
  38. for (var key in this.UNITS) {
  39. if (milliseconds >= this.UNITS[key]) {
  40. humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
  41. break;
  42. }
  43. }
  44. return humanize || '刚刚';
  45. },
  46. format: function (dateStr) {
  47. var date = this.parse(dateStr)
  48. var diff = Date.now() - date.getTime();
  49. if (diff < this.UNITS['天']) {
  50. return this.humanize(diff);
  51. }
  52. var _format = function (number) {
  53. return (number < 10 ? ('0' + number) : number);
  54. };
  55. return date.getFullYear() + '-' + _format(date.getMonth() + 1) + '-' + _format(date.getDay()) + '-' +
  56. _format(date.getHours()) + ':' + _format(date.getMinutes());
  57. },
  58. parse: function (str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串,转化为一个Date对象
  59. var a = str.split(/[^0-9]/);
  60. return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
  61. }
  62. };
  63. function yzyTime(cjsj) {
  64. var date = new Date(cjsj * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
  65. var Y = date.getFullYear() + '-';
  66. var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  67. var D = (date.getDate() + 1 <= 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
  68. var h = (date.getHours() + 1 <= 10 ? '0' + (date.getHours()) : date.getHours()) + ':';
  69. var m = (date.getMinutes() + 1 <= 10 ? '0' + (date.getMinutes()) : date.getMinutes()) + ':';
  70. var s = (date.getSeconds() + 1 <= 10 ? '0' + (date.getSeconds()) : date.getSeconds()) + '';
  71. return Y + M + D + h + m + s;
  72. }
  73. function yzyTimes(cjsj) {
  74. var date = new Date(cjsj * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
  75. var Y = date.getFullYear() + '-';
  76. var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  77. var D = (date.getDate() + 1 <= 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
  78. var h = (date.getHours() + 1 <= 10 ? '0' + (date.getHours()) : date.getHours()) + ':';
  79. var m = (date.getMinutes() + 1 <= 10 ? '0' + (date.getMinutes()) : date.getMinutes()) + ':';
  80. var s = (date.getSeconds() + 1 <= 10 ? '0' + (date.getSeconds()) : date.getSeconds()) + '';
  81. return Y + M + D;
  82. }
  83. module.exports = {
  84. yzyTime:yzyTime,
  85. yzyTimes:yzyTimes,
  86. formatTime:formatTime,
  87. formatLocation:formatLocation,
  88. dateUtils:dateUtils
  89. }