index.js 2.93 KB
Newer Older
潘琦 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
/**
 * 取消滚动条
 * @param {*} s
 */
export function noscroll () {
  document.getElementById('app').classList.add('noscroll')
}

/**
 * 恢复滚动条
 * @param {*} s
 */
export function renewscroll () {
  document.getElementById('app').classList.remove('noscroll')
}

/**
 * 根据身份证获取性别
 * @param {*} s
 */
export function getSexByIdCard (idNumber) {
  let sex = '3'
  if (idNumber && idNumber !== '' && idNumber.length === 18) {
    if (idNumber.substring(16, 17) % 2 === 0) {
      sex = '2'
    } else {
      sex = '1'
    }
  }
  return sex
}

/**
 * 根据身份证获取生日
 * @param {*} s
 */
export function getBirthByIdNumber (idNumber) {
  let birth = ''
  if (idNumber.length === 18) {
    birth = idNumber.substring(6, 14)
  } else if (idNumber.length === 15) {
    birth = '19' + idNumber.substring(6, 12)
  }
  if (birth !== '') {
    birth = birth.replace(/(.{4})(.{2})/, '$1-$2-')
  }
  return birth
}

/**
 * 根据出生年月获取年龄
 * @param {*} s
 */
export function getAgeByBirth (birth) {
  console.log('getAgeByBirth')
  if (!birth || birth === '') {
    return ''
  }
  const nowDate = new Date()
  let year = nowDate.getFullYear()
  let month = nowDate.getMonth() + 1
  let day = nowDate.getDate()

  let birthYear = parseInt(birth.split('-')[0])
  let birthMonth = parseInt(birth.split('-')[1])
  let birthDay = parseInt(birth.split('-')[2])

  let differYear = -1
  let differMonth = -1
  let differDay = -1
  let age = ''
  if (year === birthYear) {
    if (month === birthMonth) {
      differDay = day - birthDay
    } else if (month > birthMonth) {
      differYear = 0
      if (day > birthDay) {
        differMonth = month - birthMonth
        differDay = day - birthDay
      } else if (day < birthDay) {
        differMonth = month - birthMonth - 1
        differDay = new Date(birthYear, birthMonth, 0).getDate() - birthDay + day
      } else {
        differMonth = month - birthMonth
        differDay = 0
      }
    }
  } else if (year > birthYear) {
    differYear = year - birthYear
    if (month > birthMonth) {
      differMonth = month - birthMonth
    } else if (month < birthMonth) {
      differYear = differYear - 1
      differMonth = 12 - (birthMonth - month)
    } else {
      if (day > birthDay) {
        differMonth = birthMonth - month
      } else if (day < birthDay) {
        differYear = differYear - 1
        differMonth = 11
      } else {
        differMonth = birthMonth - month
      }
    }
  }

  if (differYear >= 0 && differMonth >= 0) {
    if (differYear > 0) {
      age = differYear + '岁'
    }
    if (differMonth > 0) {
      age = age + differMonth + '月'
    }
    if (differDay > 0) {
      age = age + differDay + '天'
    }
  } else {
    age = differDay + '天'
  }

  return age
}

/**
 * 计算当天之前多少天日期
 * @param {*} s
 */
export function SubtractDay (day) {
  let date = new Date() // 获取当前时间
  date.setDate(date.getDate() - day)
  return date
}