util.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. //获取两日期之间日期列表函数
  28. function getdiffdate(stime, etime) {
  29. //初始化日期列表,数组
  30. var diffdate = new Array();
  31. var i = 0;
  32. //开始日期小于等于结束日期,并循环
  33. while (stime <= etime) {
  34. diffdate[i] = stime;
  35. //获取开始日期时间戳
  36. var stime_ts = new Date(stime).getTime();
  37. //增加一天时间戳后的日期
  38. var next_date = stime_ts + (24 * 60 * 60 * 1000);
  39. //拼接年月日,这里的月份会返回(0-11),所以要+1
  40. var next_dates_y = new Date(next_date).getFullYear() + '-';
  41. var next_dates_m = (new Date(next_date).getMonth() + 1 < 10) ? '0' + (new Date(next_date).getMonth() + 1) +
  42. '-' : (new Date(next_date).getMonth() + 1) + '-';
  43. var next_dates_d = (new Date(next_date).getDate() < 10) ? '0' + new Date(next_date).getDate() : new Date(
  44. next_date).getDate();
  45. stime = next_dates_y + next_dates_m + next_dates_d;
  46. //增加数组key
  47. i++;
  48. }
  49. return diffdate;
  50. }
  51. /**
  52. * 计算两个日期之间的天数
  53. * @param dateString1 开始日期 yyyy-MM-dd
  54. * @param dateString2 结束日期 yyyy-MM-dd
  55. * @returns {number} 如果日期相同 返回一天 开始日期大于结束日期,返回0
  56. */
  57. const getDaysBetween = (dateString1, dateString2) => {
  58. let startDate = Date.parse(dateString1);
  59. let endDate = Date.parse(dateString2);
  60. if (startDate > endDate) {
  61. return 0;
  62. }
  63. if (startDate == endDate) {
  64. return 1;
  65. }
  66. let days = (endDate - startDate) / (1 * 24 * 60 * 60 * 1000);
  67. return days;
  68. }
  69. /**
  70. * 乘法函数,用来得到精确的乘法结果
  71. * @param {Object} arg1
  72. * @param {Object} arg2
  73. */
  74. const accMul = (arg1, arg2) => {
  75. var m = 0;
  76. m += deal(arg1);
  77. m += deal(arg2);
  78. var r1 = Number(arg1.toString().replace('.', ''));
  79. var r2 = Number(arg2.toString().replace('.', ''));
  80. return (r1 * r2) / Math.pow(10, m);
  81. }
  82. /**
  83. * 加法函数,用来得到精确的加法结果
  84. * @param {Object} arg1
  85. * @param {Object} arg2
  86. */
  87. const accAdd = (arg1, arg2) => {
  88. var r1 = deal(arg1);
  89. var r2 = deal(arg2);
  90. var m = Math.pow(10, Math.max(r1, r2));
  91. return (arg1 * m + arg2 * m) / m;
  92. }
  93. /**
  94. * 除法函数,用来得到精确的除法结果
  95. * @param {Object} arg1
  96. * @param {Object} arg2
  97. */
  98. const accDiv = (arg1, arg2) => {
  99. var t1 = deal(arg1);
  100. var t2 = deal(arg2);
  101. var r1 = Number(arg1.toString().replace(".", ""))
  102. var r2 = Number(arg2.toString().replace(".", ""))
  103. return (r1 / r2) * Math.pow(10, t2 - t1);
  104. }
  105. /**
  106. * 求小数点后的数据长度
  107. */
  108. const deal = (arg) => {
  109. var t = 0;
  110. try {
  111. t = arg.toString().split('.')[1].length;
  112. } catch (e) {}
  113. return t;
  114. }
  115. /**
  116. * 判断 有效日期不能小于今天
  117. */
  118. const dateCompare = (beginDate, endDate) => {
  119. var beginArrayDate = beginDate.split('-');
  120. var endArrayDate = endDate.split('-')
  121. if (parseInt(endArrayDate[0], 10) > parseInt(beginArrayDate[0], 10)) return true;
  122. if (parseInt(endArrayDate[0], 10) == parseInt(beginArrayDate[0], 10)) {
  123. if (parseInt(endArrayDate[1], 10) > parseInt(beginArrayDate[1], 10)) return true;
  124. else {
  125. if (parseInt(endArrayDate[1], 10) == parseInt(beginArrayDate[1], 10)) {
  126. if (parseInt(endArrayDate[2], 10) >= parseInt(beginArrayDate[2], 10)) return true;
  127. }
  128. }
  129. }
  130. return false;
  131. }
  132. /**
  133. * 获取日期
  134. * day 获取日期
  135. * time 获取时间
  136. */
  137. const getDate = (obj = 'day') => {
  138. const date = new Date();
  139. let year = date.getFullYear();
  140. let month = date.getMonth() + 1;
  141. let day = date.getDate();
  142. let Hours = date.getHours();
  143. let Minutes = date.getMinutes();
  144. month = month > 9 ? month : '0' + month;
  145. day = day > 9 ? day : '0' + day;
  146. Hours = Hours > 9 ? Hours : '0' + Hours;
  147. Minutes = Minutes > 9 ? Minutes : '0' + Minutes;
  148. if (obj == 'year') {
  149. return `${year}`;
  150. }
  151. if (obj == 'month') {
  152. return `${month}`;
  153. }
  154. if (obj == 'day') {
  155. return `${year}-${month}-${day}`;
  156. }
  157. if (obj == 'time') {
  158. return `${year}-${month}-${day}` + ' ' + Hours + ':' + Minutes;
  159. }
  160. }
  161. /**
  162. * 一些固定的基础数据
  163. */
  164. const getData = (obj = 'education') => {
  165. //性别
  166. if (obj == 'state') {
  167. return ["离职-随时到岗", "在职-月内到岗", "在职-考虑机会", "在职-暂不考虑"]
  168. }
  169. //性别
  170. if (obj == 'sex') {
  171. return ["男", "女"]
  172. }
  173. //学历
  174. if (obj == 'qualification') {
  175. return ["初中及以下", "中专/中技", "高中", "大专", "本科", "硕士", "博士"]
  176. }
  177. //求职类型
  178. if (obj == 'type') {
  179. return ["全职", "兼职"]
  180. }
  181. //工作经验
  182. if (obj == 'experience') {
  183. return ["不限", "1年以内", "1-3年", "3-5年", "5-10年", "10年以上"]
  184. }
  185. //职位要求
  186. if (obj == 'positionName') {
  187. return ["不限", "选择职位"]
  188. }
  189. //工作地点
  190. if (obj == 'address') {
  191. return ["不限", "选择工作地点"]
  192. }
  193. //人员规模
  194. if (obj == 'nums') {
  195. return ["0-20人", "20-99人", "100-499人", "500-999人", "1000-9999人", "10000人以上"]
  196. }
  197. // 结算方式
  198. if (obj == 'unit') {
  199. return ["日结", "单结", ]
  200. }
  201. }
  202. const toRadians = (degree) => {
  203. return degree * Math.PI / 180;
  204. }
  205. const distance = (latitude1, longitude1, latitude2, longitude2) => {
  206. var R = 6371;
  207. var deltaLatitude = toRadians(latitude2 - latitude1);
  208. var deltaLongitude = toRadians(longitude2 - longitude1);
  209. latitude1 = toRadians(latitude1);
  210. latitude2 = toRadians(latitude2);
  211. var a = Math.sin(deltaLatitude / 2) *
  212. Math.sin(deltaLatitude / 2) +
  213. Math.cos(latitude1) *
  214. Math.cos(latitude2) *
  215. Math.sin(deltaLongitude / 2) *
  216. Math.sin(deltaLongitude / 2);
  217. var c = 2 * Math.atan2(Math.sqrt(a),
  218. Math.sqrt(1 - a));
  219. var d = R * c;
  220. return d;
  221. }
  222. /**
  223. * 格式化时间
  224. */
  225. const format = (obj) => {
  226. const date = new Date(obj);
  227. let year = date.getFullYear();
  228. let month = date.getMonth() + 1;
  229. let day = date.getDate();
  230. let Hours = date.getHours();
  231. let Minutes = date.getMinutes();
  232. month = month > 9 ? month : '0' + month;
  233. day = day > 9 ? day : '0' + day;
  234. Hours = Hours > 9 ? Hours : '0' + Hours;
  235. Minutes = Minutes > 9 ? Minutes : '0' + Minutes;
  236. return `${year}-${month}-${day}` + ' ' + Hours + ':' + Minutes;
  237. }
  238. module.exports = {
  239. formatTime: formatTime,
  240. formatLocation: formatLocation,
  241. accMul: accMul,
  242. accAdd: accAdd,
  243. accDiv: accDiv,
  244. dateCompare: dateCompare,
  245. getDate: getDate,
  246. getDaysBetween: getDaysBetween,
  247. getdiffdate: getdiffdate,
  248. getData: getData,
  249. distance: distance
  250. }