util.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. const days = (date1, date2) => {
  70. // 将日期字符串转换为 Date 对象
  71. const startDate = new Date(date1);
  72. const endDate = new Date(date2);
  73. // 计算两个日期之间的毫秒数差
  74. const timeDifference = endDate - startDate;
  75. // 将毫秒数转换为天数
  76. const dayDifference = Math.ceil(timeDifference / (1000 * 60 * 60 * 24));
  77. // 如果不足一天,取1天
  78. return Math.max(1, dayDifference);
  79. }
  80. /**
  81. * 乘法函数,用来得到精确的乘法结果
  82. * @param {Object} arg1
  83. * @param {Object} arg2
  84. */
  85. const accMul = (arg1, arg2) => {
  86. var m = 0;
  87. m += deal(arg1);
  88. m += deal(arg2);
  89. var r1 = Number(arg1.toString().replace('.', ''));
  90. var r2 = Number(arg2.toString().replace('.', ''));
  91. return (r1 * r2) / Math.pow(10, m);
  92. }
  93. /**
  94. * 加法函数,用来得到精确的加法结果
  95. * @param {Object} arg1
  96. * @param {Object} arg2
  97. */
  98. const accAdd = (arg1, arg2) => {
  99. var r1 = deal(arg1);
  100. var r2 = deal(arg2);
  101. var m = Math.pow(10, Math.max(r1, r2));
  102. return (arg1 * m + arg2 * m) / m;
  103. }
  104. /**
  105. * 除法函数,用来得到精确的除法结果
  106. * @param {Object} arg1
  107. * @param {Object} arg2
  108. */
  109. const accDiv = (arg1, arg2) => {
  110. var t1 = deal(arg1);
  111. var t2 = deal(arg2);
  112. var r1 = Number(arg1.toString().replace(".", ""))
  113. var r2 = Number(arg2.toString().replace(".", ""))
  114. return (r1 / r2) * Math.pow(10, t2 - t1);
  115. }
  116. /**
  117. * 求小数点后的数据长度
  118. */
  119. const deal = (arg) => {
  120. var t = 0;
  121. try {
  122. t = arg.toString().split('.')[1].length;
  123. } catch (e) {}
  124. return t;
  125. }
  126. /**
  127. * 判断 有效日期不能小于今天
  128. */
  129. const dateCompare = (beginDate, endDate) => {
  130. var beginArrayDate = beginDate.split('-');
  131. var endArrayDate = endDate.split('-')
  132. if (parseInt(endArrayDate[0], 10) > parseInt(beginArrayDate[0], 10)) return true;
  133. if (parseInt(endArrayDate[0], 10) == parseInt(beginArrayDate[0], 10)) {
  134. if (parseInt(endArrayDate[1], 10) > parseInt(beginArrayDate[1], 10)) return true;
  135. else {
  136. if (parseInt(endArrayDate[1], 10) == parseInt(beginArrayDate[1], 10)) {
  137. if (parseInt(endArrayDate[2], 10) >= parseInt(beginArrayDate[2], 10)) return true;
  138. }
  139. }
  140. }
  141. return false;
  142. }
  143. /**
  144. * 获取日期
  145. * day 获取日期
  146. * time 获取时间
  147. */
  148. const getDate = (obj = 'day') => {
  149. const date = new Date();
  150. let year = date.getFullYear();
  151. let month = date.getMonth() + 1;
  152. let day = date.getDate();
  153. let Hours = date.getHours();
  154. let Minutes = date.getMinutes();
  155. month = month > 9 ? month : '0' + month;
  156. day = day > 9 ? day : '0' + day;
  157. Hours = Hours > 9 ? Hours : '0' + Hours;
  158. Minutes = Minutes > 9 ? Minutes : '0' + Minutes;
  159. if (obj == 'year') {
  160. return `${year}`;
  161. }
  162. if (obj == 'month') {
  163. return `${month}`;
  164. }
  165. if (obj == 'day') {
  166. return `${year}-${month}-${day}`;
  167. }
  168. if (obj == 'time') {
  169. return `${year}-${month}-${day}` + ' ' + Hours + ':' + Minutes;
  170. }
  171. }
  172. /**
  173. * 一些固定的基础数据
  174. */
  175. const getData = (obj = 'education') => {
  176. //性别
  177. if (obj == 'state') {
  178. return ["离职-随时到岗", "在职-月内到岗", "在职-考虑机会", "在职-暂不考虑"]
  179. }
  180. //性别
  181. if (obj == 'sex') {
  182. return ["男", "女"]
  183. }
  184. //学历
  185. if (obj == 'qualification') {
  186. return ["初中及以下", "中专/中技", "高中", "大专", "本科", "硕士", "博士"]
  187. }
  188. //求职类型
  189. if (obj == 'type') {
  190. return ["全职", "兼职"]
  191. }
  192. //工作经验
  193. if (obj == 'experience') {
  194. return ["不限", "1年以内", "1-3年", "3-5年", "5-10年", "10年以上"]
  195. }
  196. //职位要求
  197. if (obj == 'positionName') {
  198. return ["不限", "选择职位"]
  199. }
  200. //工作地点
  201. if (obj == 'address') {
  202. return ["不限", "选择工作地点"]
  203. }
  204. //人员规模
  205. if (obj == 'nums') {
  206. return ["0-20人", "20-99人", "100-499人", "500-999人", "1000-9999人", "10000人以上"]
  207. }
  208. // 结算方式
  209. if (obj == 'unit') {
  210. return ["日结", "单结", ]
  211. }
  212. }
  213. const toRadians = (degree) => {
  214. return degree * Math.PI / 180;
  215. }
  216. const distance = (latitude1, longitude1, latitude2, longitude2) => {
  217. var R = 6371;
  218. var deltaLatitude = toRadians(latitude2 - latitude1);
  219. var deltaLongitude = toRadians(longitude2 - longitude1);
  220. latitude1 = toRadians(latitude1);
  221. latitude2 = toRadians(latitude2);
  222. var a = Math.sin(deltaLatitude / 2) *
  223. Math.sin(deltaLatitude / 2) +
  224. Math.cos(latitude1) *
  225. Math.cos(latitude2) *
  226. Math.sin(deltaLongitude / 2) *
  227. Math.sin(deltaLongitude / 2);
  228. var c = 2 * Math.atan2(Math.sqrt(a),
  229. Math.sqrt(1 - a));
  230. var d = R * c;
  231. return d;
  232. }
  233. /**
  234. * 格式化时间
  235. */
  236. const format = (obj) => {
  237. const date = new Date(obj);
  238. let year = date.getFullYear();
  239. let month = date.getMonth() + 1;
  240. let day = date.getDate();
  241. let Hours = date.getHours();
  242. let Minutes = date.getMinutes();
  243. month = month > 9 ? month : '0' + month;
  244. day = day > 9 ? day : '0' + day;
  245. Hours = Hours > 9 ? Hours : '0' + Hours;
  246. Minutes = Minutes > 9 ? Minutes : '0' + Minutes;
  247. return `${year}-${month}-${day}` + ' ' + Hours + ':' + Minutes;
  248. }
  249. module.exports = {
  250. formatTime: formatTime,
  251. formatLocation: formatLocation,
  252. accMul: accMul,
  253. accAdd: accAdd,
  254. accDiv: accDiv,
  255. dateCompare: dateCompare,
  256. getDate: getDate,
  257. getDaysBetween: getDaysBetween,
  258. getdiffdate: getdiffdate,
  259. getData: getData,
  260. distance: distance,
  261. days: days
  262. }