mf.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. export function getTime2() {
  2. let d = new Date();
  3. let hours = d.getHours()
  4. let minutes = d.getMinutes()
  5. let seconds = d.getSeconds()
  6. hours = hours > 9 ? hours + '' : '0' + hours
  7. minutes = minutes > 9 ? minutes + '' : '0' + minutes
  8. seconds = seconds > 9 ? seconds + '' : '0' + seconds
  9. return hours + minutes + seconds
  10. }
  11. export function getDate2() {
  12. let d = new Date()
  13. let year = d.getFullYear()
  14. let month = d.getMonth() + 1
  15. let day = d.getDate()
  16. return year + getFormatDate(month) + getFormatDate(day)
  17. }
  18. export function getDate() {
  19. let d = new Date();
  20. let year = d.getFullYear();
  21. let month = d.getMonth() + 1;
  22. let day = d.getDate();
  23. return year + '-' + getFormatDate(month) + '-' + getFormatDate(day);
  24. }
  25. /**
  26. * 获取生日
  27. *
  28. * @param IDCard
  29. * @returns {string}
  30. */
  31. let getBirthDate = function (idCard) {
  32. let year = idCard.substring(6, 10);
  33. let month = idCard.substring(10, 12);
  34. let day = idCard.substring(12, 14);
  35. return year + '-' + month + '-' + day;
  36. }
  37. /**
  38. * 获取当前时间
  39. *
  40. * @returns {string}
  41. */
  42. let getNowDate = function () {
  43. let d = new Date();
  44. let year = d.getFullYear();
  45. let month = d.getMonth() + 1;
  46. let day = d.getDate();
  47. return year + '-' + getFormatDate(month) + '-' + getFormatDate(day);
  48. }
  49. /**
  50. * 格式化时间
  51. *
  52. * @param value
  53. * @returns {string}
  54. */
  55. let getFormatDate = function (value) {
  56. if (value === undefined || value === "") {
  57. return '';
  58. }
  59. let str = value;
  60. if (parseInt(value) < 10) {
  61. str = '0' + value;
  62. }
  63. return str;
  64. }
  65. /**
  66. * 获取年龄
  67. *
  68. * @param startDateStr
  69. * @param endDateStr
  70. * @returns {number}
  71. */
  72. let getAge = function (startDateStr, endDateStr) {
  73. //计算两个日期相差多少年
  74. let startDate = new Date(startDateStr);
  75. let endDate = new Date(endDateStr);
  76. let yearNum = endDate.getFullYear() - startDate.getFullYear()-1;
  77. //获取两个日期(月+日)部分
  78. let sStr = startDateStr.substring(5, 10);
  79. let eStr = endDateStr.substring(5, 10);
  80. //判断两个日期大小
  81. //判断是否过生日
  82. if (new Date(sStr) <= new Date(eStr)) {
  83. return yearNum + 1;
  84. }
  85. else {
  86. return yearNum;
  87. }
  88. }
  89. export default {
  90. getIDCardInfo(idCard) {
  91. if (idCard.length === 18) {
  92. //获取计算后出生日期
  93. let birthDateStr = getBirthDate(idCard);
  94. //设置出生日期
  95. // console.log(birthDateStr);
  96. //获取当前的日期
  97. var nowDateStr = getNowDate();
  98. //获取计算后年龄(两个日期之间)
  99. var age = getAge(birthDateStr, nowDateStr);
  100. let sex = idCard.substring(16, 17);
  101. // console.log(sex%2 ===0)
  102. //设置年龄
  103. return { birthDateStr: birthDateStr, age: age, sex: sex % 2 === 0 ? '2' : '1' }
  104. }
  105. },
  106. clickNavto: (path, type) => {
  107. // console.log(path, type)
  108. if (type == 'redirect') {
  109. uni.redirectTo({
  110. url: path
  111. })
  112. }
  113. if (type == 'switchTab') {
  114. uni.switchTab({
  115. url: path
  116. })
  117. }
  118. if (type == 'navigateBack') {
  119. uni.navigateBack({
  120. delta: 1
  121. })
  122. }
  123. if (!type) {
  124. uni.navigateTo({
  125. url: path
  126. })
  127. }
  128. }
  129. }