根据出生日期转换岁月天

            getDateValue(entry){
                var year, month,day;//差值
                //出生日期
                var dateArr = entry.split("-");
                var entryYear = parseInt(dateArr[0]);
                var entryMonth = parseInt(dateArr[1]);
                var entryDay = parseInt(dateArr[2]);
                //当前日期
                var d = new Date();
                var nowYear = d.getFullYear();
                var nowDay = d.getDate();
                var nowMonth = d.getMonth() + 1;
                //得到天
                if(nowDay >= entryDay){
                    day = nowDay - entryDay;
                }else{
                    nowMonth = nowMonth - 1;
                    day = this.getDay(nowMonth,nowYear) + nowDay - entryDay;
                }
                //得到月
                if(nowMonth >= entryMonth){
                    month = nowMonth - entryMonth;
                }else{
                    nowYear = nowYear -1;
                    month = 12 + nowMonth - entryMonth;
                }
                //得到年
                year = nowYear - entryYear;
                //字符串
                var result='';
                if (year != 0)
                    result += Math.abs(year) + "岁";
                if (month != 0)
                    result += Math.abs(month) + "月";
                if (day != 0)
                    result += Math.abs(day) + "天";
                return result;
            },
            getDay(month,year){
                let day = 0;
                switch (month){
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        day = 31;
                        break;
                    case 2:
                        //闰年天,平年天
                        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
                            day = 29;
                        }else{
                            day = 28;
                        }
                        break;
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                        day = 30;
                        break;
                    }
                    return day;
            },

 

posted @ 2020-10-21 19:40  曦雨幽兰  阅读(313)  评论(0编辑  收藏  举报