【js】根据出生日期算出年龄,获取最近几天日期,实现身份证计算生日,性别,年龄,深度比较两个对象是否相同

/* 根据出生日期算出年龄*/
export function getAgeBirth(strBirthday) {
  let returnAge
  const strBirthdayArr = strBirthday.split('-')
  const birthYear = strBirthdayArr[0]
  const birthMonth = strBirthdayArr[1]
  const birthDay = strBirthdayArr[2]

  const d = new Date()
  const nowYear = d.getFullYear()
  const nowMonth = d.getMonth() + 1
  const nowDay = d.getDate()

  if (nowYear === birthYear) {
    returnAge = 0// 同年 则为0岁
  } else {
    const ageDiff = nowYear - birthYear // 年之差
    if (ageDiff > 0) {
      if (nowMonth === birthMonth) {
        const dayDiff = nowDay - birthDay// 日之差
        if (dayDiff < 0) {
          returnAge = ageDiff - 1
        } else {
          returnAge = ageDiff
        }
      } else {
        const monthDiff = nowMonth - birthMonth// 月之差
        if (monthDiff < 0) {
          returnAge = ageDiff - 1
        } else {
          returnAge = ageDiff
        }
      }
    } else {
      returnAge = '0'// 返回-1 表示出生日期输入错误 晚于今天
    }
  }
  return returnAge// 返回周岁年龄
}

// 获取最近几天日期
export function timeForMat(count) {
  const date = new Date()
  const nowDate = processingTime()
  const LDate = new Date(date - 1000 * 60 * 60 * 24 * count)
  const year = LDate.getFullYear()
  const month = LDate.getMonth() + 1
  const day = LDate.getDate()
  const temp = {
    startTime: year + '/' + (month < 10 ? '0' + month : month) + '/' + (day < 10 ? '0' + day : day),
    endTime: nowDate
  }
  return temp
}

// 实现自动生成生日,性别,年龄
export function go(val) {
  const iden = val
  let sex = null
  let birth = null
  if (val.length === 18) {
    sex = iden.substring(16, 17)
    birth = iden.substring(6, 10) + '/' + iden.substring(10, 12) + '/' + iden.substring(12, 14)
  }
  if (val.length === 15) {
    sex = iden.substring(13, 14)
    birth = '19' + iden.substring(6, 8) + '/' + iden.substring(8, 10) + '/' + iden.substring(10, 12)
  }
  if (sex % 2 === 0) { sex = '女' } else { sex = '男' }
  return {
    sex,
    birth
  }
}


/**
 * 判断此对象是否是Object类型
 * @param {Object} obj
 */
function isObject(obj) {
  return Object.prototype.toString.call(obj) === '[object Object]'
}
/**
 * 判断此类型是否是Array类型
 * @param {Array} arr
 */
function isArray(arr) {
  return Object.prototype.toString.call(arr) === '[object Array]'
}
/**
 *  深度比较两个对象是否相同
 * @param {Object} oldData
 * @param {Object} newData
 */
export function equalsObj(oldData, newData) {
  // 类型为基本类型时,如果相同,则返回true
  if (oldData === newData) return true
  if (isObject(oldData) && isObject(newData) && Object.keys(oldData).length === Object.keys(newData).length) {
    // 类型为对象并且元素个数相同

    // 遍历所有对象中所有属性,判断元素是否相同
    for (const key in oldData) {
      if (oldData.hasOwnProperty(key)) {
        if (!equalsObj(oldData[key], newData[key])) {
          // 对象中具有不相同属性 返回false
          return false
        }
      }
    }
  } else if (isArray(oldData) && isArray(newData) && oldData.length === newData.length) {
    // 类型为数组并且数组长度相同

    for (let i = 0, length = oldData.length; i < length; i++) {
      if (!equalsObj(oldData[i], newData[i])) {
      // 如果数组元素中具有不相同元素,返回false
        return false
      }
    }
  } else {
    // 其它类型,均返回false
    return false
  }

  // 走到这里,说明数组或者对象中所有元素都相同,返回true
  return true
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沙滩上的一颗石头

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值