util.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. formateDate: function(newdate, format) {
  63. const date = {
  64. 'M+': newdate.getMonth() + 1,
  65. 'd+': newdate.getDate(),
  66. 'h+': newdate.getHours(),
  67. 'm+': newdate.getMinutes(),
  68. 's+': newdate.getSeconds(),
  69. 'q+': Math.floor((newdate.getMonth() + 3) / 3),
  70. 'S+': newdate.getMilliseconds()
  71. };
  72. if (/(y+)/i.test(format)) {
  73. format = format.replace(RegExp.$1, (newdate.getFullYear() + '').substr(4 - RegExp.$1.length));
  74. }
  75. for (const k in date) {
  76. if (new RegExp('(' + k + ')').test(format)) {
  77. format = format.replace(RegExp.$1, RegExp.$1.length === 1 ?
  78. date[k] : ('00' + date[k]).substr(('' + date[k]).length));
  79. }
  80. }
  81. return format;
  82. },
  83. getFirstDayOfWeek: function(date) {
  84. var day = date.getDay() || 7;
  85. var result = new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1 - day);
  86. return result;
  87. },
  88. getLastDayOfWeek: function(date) {
  89. var day = date.getDay() || 7;
  90. var result = new Date(date.getFullYear(), date.getMonth(), date.getDate() + 7 - day);
  91. return result;
  92. },
  93. /**获取当年第一天 */
  94. getCurrentYearFirstDate: function(now) {
  95. const nowyear = now.getFullYear() + ''; // 获取年(yyyy)
  96. // 调用格式化方法
  97. const firstDate = new Date(nowyear + '/01' + '/01');
  98. return firstDate;
  99. },
  100. /**获取当年最后一天 */
  101. getCurrentYearLastDate: function(now) {
  102. const year = now.getFullYear() + 1; // 获取年(yyyy)
  103. // 调用格式化方法
  104. const nextyear = new Date(year + '/01' + '/01');
  105. nextyear.setDate(nextyear.getDate() - 1);
  106. return nextyear;
  107. },
  108. /**获取当月月第一天 */
  109. getCurrentMonFirstDate: function(now) {
  110. const nowyear = now.getFullYear() + ''; // 获取年(yyyy)
  111. let nowmonth = now.getMonth() + 1 + ''; // 获取月份(0-11,0代表1月)
  112. // 调用格式化方法
  113. nowmonth = this.dateformat(nowmonth);
  114. const firstDate = new Date(nowyear + '/' + nowmonth + '/01');
  115. return firstDate;
  116. },
  117. /**获取当月月最后一天 */
  118. getCurrentMonLastDate: function(now) {
  119. const nextMonFirstDate = this.getNextMonFirstDate(now);
  120. nextMonFirstDate.setDate(nextMonFirstDate.getDate() - 1);
  121. return nextMonFirstDate;
  122. },
  123. /**获取下个月第一天 */
  124. getNextMonFirstDate: function(now) {
  125. const currentMonFirstDate = this.getCurrentMonFirstDate(now);
  126. currentMonFirstDate.setMonth(currentMonFirstDate.getMonth() + 1);
  127. return currentMonFirstDate;
  128. },
  129. dateformat: function(fmt) {
  130. if (fmt.length === 1) {
  131. fmt = '0' + fmt;
  132. }
  133. return fmt;
  134. }
  135. };
  136. module.exports = {
  137. formatTime: formatTime,
  138. formatLocation: formatLocation,
  139. dateUtils: dateUtils
  140. }