「兔了个兔」教你一键查询任意年份的属相和干支!!!!

186 阅读2分钟

我正在参加「兔了个兔」创意投稿大赛,详情请看:「兔了个兔」创意投稿大赛

前言

我们都知道今年是兔年,那是如何得出的呢?如果给我们任意一个年份,我们怎么知道他到底是什么年呢?

我们常常有这种困惑,今年是什么年来着,总是稀里糊涂的过了一年又一年。举例:2023年是农历癸卯年(兔年)。是怎么得到这个结果的呢,有没有什么方法可以查到呢?我们查阅资料得到了答案。

干支纪年法

干支纪年法是根据十天干和十二地支的组合来纪年的。10和12的最小公倍数是60,所以干支纪年就会出现“六十一甲子”的现象了。

天干定位--尾数定天干

屏幕快照 2023-01-31 上午9.47.07.png

也可以通过计算公式来算:(年份)/ 10的余数

举例: 2023年尾数是3,对应的天干就是癸

地支定位

生肖定地支

地支有12个,定位起来有些复杂。我们知道我国是有12生肖的,而这十二生肖与十二地支存在着一一对应关系。

屏幕快照 2023-01-31 上午9.47.18.png

举例:2023年是兔年,所以对应的地支是

余数定地支

生肖定地支虽然简单,但是我们不可能记住所有年份的属相。

由于地支是十二个,所以每隔十二年的地支应该是一样的,如果我们知道某一个年份的地支,则可以通过计算得出其他年份的地支。

地支的计算方法:年份 / 12的余数

屏幕快照 2023-01-31 上午9.47.27.png

举例:2023年

天干:2023 / 10 = 202 余数3 对应癸

地支:2023 / 12 = 168 余数7 对应卯, 卯对应属相兔

故2023年是农历癸卯兔年

代码实现(使用vue2)

需求

输入年份直接得出干支和属相

部分代码

export default {
  data() {
    return {
      year: 2023,
      sky: '', 
      land:'', 
      animal: '',
      flag: false
    };
  },
  methods: {
    handleFocus() {
      // 输入框获取焦点后将上次查询的结果清空
      this.flag = false
    },
    search() {
      // 天干对应关系
      const skyArr = [{
        index: 4,
        name: '甲'
      },{
        index: 5,
        name: '乙'
      },{
        index: 6,
        name: '丙'
      },{
        index: 7,
        name: '丁'
      },{
        index: 8,
        name: '戊'
      },{
        index: 9,
        name: '己'
      },{
        index: 0,
        name: '庚'
      },{
        index: 1,
        name: '辛'
      },{
        index: 2,
        name: '壬'
      },{
        index: 3,
        name: '癸'
      }]
      // 地支对应关系
      const landArr = [{
        index: 4,
        name: '子',
        animal: '鼠'
      },{
        index: 5,
        name: '丑',
        animal: '牛'
      },{
        index: 6,
        name: '寅',
        animal: '虎'
      },{
        index: 7,
        name: '卯',
        animal: '兔'
      },{
        index: 8,
        name: '辰',
        animal: '龙'
      },{
        index: 9,
        name: '巳',
        animal: '蛇'
      },{
        index: 10,
        name: '午',
        animal: '马'
      },{
        index: 11,
        name: '未',
        animal: '羊'
      },{
        index: 0,
        name: '申',
        animal: '猴'
      },{
        index: 1,
        name: '酉',
        animal: '鸡'
      },{
        index: 2,
        name: '戌',
        animal: '狗'
      },{
        index: 3,
        name: '亥',
        animal: '猪'
      }]
      const reg = /^[0-9]+$/

      

      if(this.year) {
        if(!reg.test(this.year) || this.year.toString().length !== 4) {
          alert('请输入正确的年份')
          return
        }
        
        // 获取天干, 根据最后一位
        let lastNumber = +(this.year.toString().split('')[3])
        skyArr.forEach(item => {
          if(item.index === lastNumber) {
            this.sky = item.name
          }
        })
        
        // 获取地支
        const remainder = this.year % 12
        landArr.forEach(item => {
          if(item.index === remainder) {
            this.land = item.name
            this.animal = item.animal
          }
        })

        this.flag = true
        
      }
    },
  },
};

效果预览