util.js 7.7 KB

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