使用 JavaScript 根据出生日期查找星座
问题
我们需要编写一个接受日期对象的JavaScript函数。基于那个对象,我们的函数应该返回与那个生日相关的星座。
示例
以下是代码-
const date = new Date();
//截至2021年4月2日
const findSign = (date) => {
const days = [21, 20, 21, 21, 22, 22, 23, 24, 24, 24, 23, 22];
const signs = ["Aquarius", "Pisces", "Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn"];
let month = date.getMonth();
let day = date.getDate();
if(month == 0 && day <= 20){
month = 11;
}else if(day < days[month]){
month--;
};
return signs[month];
};
console.log(findSign(date));输出结果Aries
输出可能会根据我们运行函数的时间而有所不同,因为我们使用的是当前日期。
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短